What is proper way to declare require'd elements within PHP in different directories?
I make use of several 'require()' statements on my site. In some of these require statements, additional 'require' elements are placed. The issue I sometimes face however is that the directory called for the first require may be different than the second and in turn cause the site to look in the wrong place.
For example:
Let's assume the site structure has 2 main folders: X and Y. Within Y is a sub folder called Z.
A file in folder X, called page.php, makes use of a require statement for a file in Y called 'function.php'. Function.php however makes use of an additional require statement in Z called 'constants.php'.
page.php (within X) looks like:
require('../Y/function.php');
and function.php (within Y) looks like:
require('Z/constants.php');
The issue I face is that when X is essentially trying to call Z's constants file, it treats it as if the folder Z laid in its own subdirectory.
i.e. page.php makes 2 require calls require('../Y/function.php'); and require('Z/constants.php'); <- which does not exist.
I know I 开发者_运维问答could use absolute paths but I am constantly switching between windows and linux environments and it would be a giant PITA to go back and forth between the two formats.
Any suggestions?
EDIT
The strangest part is, sometimes the above issue works. I cannot figure out why quite yet but it seems like the issue may actually be that going forward in directories can be autocorrected but going backwards (i.e. being at level 3 in one folder and jumping back causes issues)
You could consider using set_include_path(get_include_path().";path\to\root");
to define a common include path. Then you can use include('Y/file.php')
and include('Z/otherfile.php');
You could set this in a base file that's always executed first before any other code is executed.
Edit: In terms of a "proper" way, if you are programming using OOP principles, then you should consider looking at using an autoloader of some sort instead. That way you've got clear naming principles for your classes, as well as the fact that you won't have to litter your code with require
and include
commands. Have a look at the SplClassLoader
in particular: https://gist.github.com/221634 Alternatively, look at building your own: http://php.net/manual/en/language.oop5.autoload.php
You are going to need absolute paths for that (unfortunately) I recommend defining something like this
define('DOCUMENT_ROOT', dirname(realpath(__FILE__)).'/');
so you can always go and change it in the future if necessary and so its easier to remember and type.
Another option is to use $_SERVER['DOCUMENT_ROOT']
instead of defining your own.
suppose you have a folder structure as following:
MainDir/X/...
MainDir/Y/...
MainDir/Z/...
this means you have X,Y and Z folders in common MainDir folder
Define a BackPath in somewhere...Lets say in X folder:
$Backpath = ../../
This will take you back to yout MainDir
then in php file
require($Backpath.'Y/function.php');
require($Backpath.'Z/constants.php');
精彩评论