How 开发者_如何学Ccan I always include other source files regardless of where the current is located in the directory structure?
Use absolute paths instead of relative ones. Preferably by using a baseURL setting somewhere in your config.
Set the include_path; that way your includes will occur invariant of the current script location.
For a PHP project, I would always set a document root for my project - eg. '/home/project' and then include all includes relative to that document root.
With a single point of entry (e.g. index.php is always the first script to be loaded), your best bet would indeed be using the include path and determining the path the path to put in there by finding the directory the current file resides in. That means that you can include files relative from the index.php file, without using a constant. The other upside is that when you move all the scripts, the include_path will be set to it's new location. Here's what I'd do.
<?php
set_include_path(
dirname( __FILE__ ) . DIRECTORY_SEPARATOR . get_include_path( )
);
?>
Now, if you don't have a single point of entry (e.g. contact.php is requested directly), you might be better of using a config-file, which will do the same as the above: change the include_path. The downside of this is that you'll have to include that config file in each and every file, but the upside is that you can move the files, without changing a single thing. Just put above code in config.php and include that in every file that is publically approachable.
There is a third way, and that's the one I always use for library files, as you can't be sure whether or not it will be used, and you can't be sure in what directory it lives. Use relatively absolute file includes. I know that sounds funny, but what I'm trying to say is that if you include files like this, you won't ever have a problem:
<?php
require_once realpath( dirname( __FILE__ ) ) . '/exception.php';
Good luck!
To get the best possible root path you should define a constant in your view file, i.e (index.php) and you should make it cross platform compatible.
after the years working in PHP and multi platform applications I discovered the best method to do this is a combination of the following native PHP Functions and constants:
- define
- str_replace
- dirname
__FILE__
Heres why we use the functions:
- The reason why we use define is to allow the document root to stay within the global scope
- The reason for
str_replace
is to change the slashes to be cross platform compatible - The reason for
dirname
is to help the relative path to the root view file - The reason for
__FILE__
is to discover the view file fordirname
We could use __DIR__
for PHP5 but __FILE__
is better as it would support previous versions of windows.
Fully combined you would have a valid relative path to your view file.
define("ROOT_PATH",str_repalce("\\","/",dirname(__FILE__)));
this would produce the perfect relative path to your index directory without the trailing slash, you should then include your files like so:
require_once ROOT_PATH . "/system/classes/some.class.php";
In windows both /
and \
are valid, and for other operating systems its just /
, so generally you should always build your applications with the /
as your directory separator.
Set define variable in your header files. then use that define variable when required like
define('BASE_PATH', $_SERVER['DOCUMENT_ROOT'].'/project/modules/module.php');
you can define $_SERVER['DOCUMENT_ROOT'] to get your document root path. then give your project name and path to that file.
精彩评论