If you don’t use namespaces, the best way is to always take into account the absolute path of the system files, and not the relative paths - that is, the path of one file relative to another.
Also, always use the following magic constants in order to facilitate the inclusion of files:
__FILE__
(to reference the current file), and
__DIR__
(to reference the current file directory).
For example:
index php.:
<?php
require_once(__DIR__ . '/bootstrap.php');
bootstrap.php:
<?php
require_once(__DIR__ . '/autoload.php');
require_once(__DIR__ . '/config/config.php');
require_once(__DIR__ . '/app/url_router.php');
Now, if you use PHP version 5.3 or higher, it’s suddenly worth starting to think about namespaces. So, instead of including the files manually, the organization of the classes takes into account the structure of directories - which is much more intuitive.
If you choose this path, it is a good use Composer, that can generate an autoload file and facilitates some aspects of your application development.
Give examples of what happens.
– Maniero
Or you add in
includepath
or uses some sort ofdefine(_ROOT_, '/caminho/padrao')
, and includes files always usinginclude _ROOT_ . "/caminho/arquivo.php";
.– Vinícius Gobbo A. de Oliveira