Appending PHP Paths

Mr. Google recommends three different methods for appending additional directories to the PHP include_path:

1. Modify php.ini to set the include path to the desired settings

2. Use .htaccess to do the same

3. Set the path from within code with set_include_path()

All of these, to me, suffer from serious problems.  What I want, as for any time I’d like to include additional functionality via code and libraries, is for those libraries to be available globally, and to be set at the meta-level of the code.  That is, the environment should determine where libraries are stored, and the code should know nothing about it.  While using set_include_path() within code isn’t objectionable, there must still be a mechanism for the code to determine what the path should be.  The code shouldn’t know or care, for example, where it is stored, let alone where the libraries it relies on are stored.

The ostensibly correct answer is to set the path in php.ini.  But because this is a setting and not a command, there’s no way to include the existing default elements of the path in addition to the libaries you’d like to include.  You could of course determine what the default values are (which are hard-coded into the compiled PHP executable) and add that to your setting, but hard-coding anything means that changes to later versions of software have now been overridden in a non-obvious way.

My solution of the day is to create a symbolic link in one of the default included directories that refers to a folder with the libraries I want.  Because this link requires a name, there’s the additional benefit that including any of my own functionality is obvious from the include line:

require_once(‘my_libraries/my_project.php’);

Leave a Reply

Your email address will not be published. Required fields are marked *