Hello guys, for a long time I wanted to enter the PHP world but I had never finished giving myself the pleasure, one of the questions I have is how to handle the include and require paths and if you have several folders to include the php files of others folders is complex.
Example if I have a configuration of my system similar to this.
The problem is moving between the routes because if I want to include the config.php file in the WorkerImp.php class I don't know how to do it because of the routes, PHP tells me that the file cannot be included.
I have read about creating a configuration file with the base routes but I come to the same conclusion to be able to access those constant routes I need to add config.php
In general
include
, how theyrequire
work like this:If the files are in the same directory as the script
If the files are in another directory
If the file is in the script's parent directory, it is put
../
in front of the file name:../
used to go back between directories.It can also be used
$_SERVER['DOCUMENT_ROOT']
as a reference point.If it
config.php
is in the root directory. You could make aninclude
of this file fromWorkerImp.php
by:Or by:
here you
../../
go back two directories from where the script is located. That is, it goes backimp>dao>
lookingconfig.php
in the directory that it finds in the root of the App.Another possibility
I have implemented the following, to remain independent of any possible route changes.
I have created a file called
dirs.php
in which I have defined all the routes of my project. I put that file in the root (root directory).Then when I need to include some file from the project:
Note that I use
include_once
, so that it does not include it again if another file in the project has done so (to understand the difference betweeninclude
einclude_once
you can consult: What is the difference between require, require_once, include, include_once in PHP?What is the advantage of using this method?
If for some reason I have to change a directory path
Dao
, I don't have to review all the parts where the includes of that directory are involved, I just change the value ofDAO_PATH
and that's it.What disadvantage?
By including the file
dirs.php
you will be including some constants that you may not use. It's also not hundreds or thousands of constants, so it won't affect your program's performance.The routes in php at first are somewhat complicated to manage, and even more so if you are not using any framework (like laravel for example) then it will be a bit more annoying.
the two options that you said in your question include and require except for an error, in this link you will be able to know more what I mean, with these you will be able to take what is there in a specified file and "paste" it in yours (by say it briefly)
While if what you want is to use the functions and others of a created class you must use the reserved word use , in this link you can find out more.
What use does is search from the root of your project for the path and name of the class
IMPORTANT NOTE
It is important that you remember that the routes in php whenever you enter a container folder you must put contra-slash( \ ) until you find your files
Here you can see more about paths and files
I hope I could help you... and good luck with PHP
Cheers!!!