-
- require_once ('.. /.. /lib/some_class.php ');
copy code This is a disadvantage: it first looks for the specified PHP include path, and then finds the current directory. Therefore, too many paths are checked. If the script is included in a script from another directory, its base directory becomes the directory where the other script resides. Another problem is that when a timed task runs the script, its parent directory may not be the working directory. Therefore, the best choice is to use an absolute path, for example:
-
- define (' ROOT ', '/var/www/project/');
- require_once (ROOT. '.. /.. /lib/some_class.php ');
copy code The above code defines an absolute path, and the value is written dead. Below, to improve, the path/var/www/project may also change, then every time you have to change it? No, you can use the __FILE__ constant, for example:
-
- define (' R OOT ', PathInfo (__file__, Pathinfo_dirname));
- require_once (ROOT. '.. /.. /lib/some_class.php ');
-
copy code Now, no matter which directory you move to, such as a server that is moved to an extranet, the code will run correctly without changes. That is, the PathInfo and __file__ constants are used to implement the portable code. |