What is the correct way to declare required elements inside PHP in different directories?

I use several require () statements on my site. Some of them require assertions; additional require elements are added. The problem that I sometimes encounter is that the directory that requires the first request may be different from the second and, in turn, forces the site to look in the wrong place.

For example:

Suppose the site structure has two main folders: X and Y. Y has a subfolder named Z.

The file in the X folder called page.php uses the require statement for the file in Y called 'function.php'. However, Function.php uses an additional require request in Z called 'constants.php'.

page.php (inside X) looks like this:

required ('../Y/function.php');

and function.php (inside Y) looks like this:

required ('Z / constants.php');

The problem I am facing is that when X essentially tries to call the Z-const file, it treats it as if the Z folder was placed in its own subdirectory.

i.e. page.php does 2 requires the required calls ('../Y/function.php'); and require ('Z / constants.php'); <- which does not exist.

I know that I can use absolute paths, but I constantly switch between windows and Linux environments, and it will be a giant PITA to go back and forth between these two formats.

Any suggestions?

EDIT

The strangest part, sometimes the problem is higher. I can’t understand why it’s pretty much the same, but it seems that the problem may be that forward directories can be automatically corrected, but go back (that is, being at level 3 in the same folder and jumping back causes problems)

0
php
source share
4 answers

You can use set_include_path(get_include_path().";path\to\root"); to determine the general include path. Then you can use include('Y/file.php') and include('Z/otherfile.php');

You can set this in the base file, which is always executed first before any other code is executed.

Edit: Regarding the β€œright” way, if you are programming using OOP principles, then you should consider using an autoloader instead. Thus, you have clear naming guidelines for your classes, as well as the fact that you don't have to clog your code with the require and include commands. Take a look at SplClassLoader in particular: https://gist.github.com/221634 As an alternative, look at creating your own: http://php.net/manual/en/language.oop5.autoload.php

+1
source share

You will need absolute paths for this (unfortunately) I recommend defining something like this

define('DOCUMENT_ROOT', dirname(realpath(__FILE__)).'/');

therefore, you can always go and change it in the future, if necessary, and therefore it is easier to remember and type.

+1
source share

Another option is to use $_SERVER['DOCUMENT_ROOT'] instead of defining your own.

+1
source share

Suppose you have a folder structure as follows:

 MainDir/X/... MainDir/Y/... MainDir/Z/... 

this means that you have folders X, Y and Z in the shared folder MainDir.

Define BackPath somewhere ... Say in folder X:

 $Backpath = ../../ 

This will get you back to yout MainDir

then in php file

 require($Backpath.'Y/function.php'); require($Backpath.'Z/constants.php'); 
+1
source share

All Articles