including files

including files

Tim Hunt發表於
Number of replies: 7
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片
Someone in the quiz forum is having a problem becuase the quiz uses require('editlib.php'); in places, and this person does not have '.' in there include_path.

Which of the following statements is correct:

1. Not having '.' in the include path is an unsupported configuration for running Moodle.

2. All includes in Moodle must be ablolute includes, starting from $CFG->libdir, or $CFG->dirroot.

If 2, we need to update the coding guidelines.
評比平均分數: -
In reply to Tim Hunt

Re: including files

Howard Miller發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片
I have always assumed (1). I didn't even realise it was possible to remove an (implied) '.' from the include path. I'm sure I have modified the (normally commented out) include path in php.ini without adding a '.' option and it has all still worked.

I'm confused now 深思的
In reply to Tim Hunt

Re: including files

Mark Nielsen發表於
Hi Tim,

Seems like 2 wouldn't work if you needed to do require('config.php');

Cheers,
Mark
In reply to Mark Nielsen

Re: including files

Tim Hunt發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片
The robust way to include config.php is

require_once(dirname(__FILE__) . '/../config.php');


with an appropriate number of ../'s
In reply to Tim Hunt

Re: including files

Howard Miller發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片

There's always (from memory)...

set_include_path( '.;' . get_include_path() );

...although it would need put several places.

In reply to Tim Hunt

Re: including files

Petr Skoda發表於
Core developers的相片 Documentation writers的相片 Particularly helpful Moodlers的相片 Peer reviewers的相片 Plugin developers的相片
That are many places with relative paths only - it means that developers expect '.' to be in include path.

Absolute paths must be used in libraries and functions. Quite ofte if you have edit.php and edit.html we are using relative paths, the dirname() might be better there.

I think the user should change PHP configuration now and we should try to use absolute paths properly in the future 眨眼

Anyway if we change coding style we might hit some new problems with PHP compatibility in various operating systems. My +1 to fix only really broken includes in libraries and function and wait with the rest till 1.8 or 2.0.