include and require do what you want it to do include_path using ini_set .
include and require do what you want it to doThis question was asked by both battlesquid and axeeOo within a day, so I thought I'd jot down an answer to it
You can run into this problem if your scripts do chdir , you can't controll your include_path or you just are paranoid that your code may be including silly things from random location s on the file system.
include_path using ini_set .You could use ini_set and ini_get to include only your code base , but you may end up breaking other libraries you're using (like PEAR).
Some other libraries assume that all their classes are in the include_path and that they will be able to get them with require_once 'class/Foo.php' . unless you are very careful about how you re-construct the include_path you will break them.
You would use something along the lines of
ini_set( 'include_path', '/path/to/me/:' . ini_get( 'include_path' ) ) ;
This is fine, but often this happens too late in the bill for it to work (particularly if you do stuff in your apache configs with auto_prepend_file directives ). Also, using ini_set can be un-friendly to other applications on your webserver, depending on the configuration of your vhosts and webserver.
The other solution (and this works particularly well if you do it from the start) is to define a constant to point to the root of your code base, and use that in all your include statements. The way to go about this is to have a core include file, that you include first in all your pages ... this file will then require all your core classes, and set up your constants, and which ever global instances of things you may want to have arround.
To do this, you'll have a directory with your code in, something like this:
your_code/ your_code/some_script.php your_code/some_other_script.php your_code/lib/ your_code/lib/core.php your_code/lib/classes/ your_code/lib/classes/Foo.php your_code/lib/classes/Bar.php
in your_code/lib/classes/Bar.php and your_code/lib/classes/Foo.php
<?php
class Foo { ... function ... }
<?php
class Bar { ... function ... }
in lib/core.php
<?php
# my code is below this directory
define('BATTLE_SQUID_CODE_BASE', dirname(__FILE__) .'/../' );
# some other things that the rest of the code base
require_once BATTLESQUID_CODE_BASE . '/lib/classes/Foo.php';
define( 'OTHER_CODEBASE_CONSTANT', Foo::get_constant('OTHER') );
$GLOBALS['_FOO_DRIVER'] = Foo::singleton();
in your_code/some_other_script.php
<?php
#suck in all the stuff we need
require_once 'lib/core.php';
# this thing is only needed by this script, so i
require_once BATTLESQUID_CODE_BASE . '/lib/Bar.class.php';
$current_bar = Bar::from_database_serial( $_GET['bar_serial'] );
As long as you make sure the first thing you include is core.php you'll have everything set up for you, and be ready to run. Also, you can have core.php do checks for your user's permissions and redirect to a must-login or no-permissions type page (if you have people logging in etc).
you are reading $Id: php-include-paths.pod 485 2007-08-03 09:42:22Z f00li5h $