I would like to create "include all" file, and then from each "real" file instead of including bunch of files each time, include just created "include all" file.
Take a look at the example -- inc.php:
<?php
require_once('A/a.php');
require_once('A/B/b.php');
?>
A/a.php:
<?php
require_once('../inc.php');
?>
A/B/b.php:
<?php
require_once("../../inc.php");
?>
However it does not work -- if I execute "a.php" I got error that "inc.php" is not found included from "b.php", and when executing "b.php" per analogy from "a.php".
I don't see what is wrong with my code -- how to solve it?
If you are using OOP, definitely go for autoloading, as suggested by cjhill.
However if you are doing something less organized, and need to include files manually, I would suggest always using the
__DIR__
constant when specifying the include path. That way include paths will be relative to the file they are defined in, rather than the entry-point file.You should think of "include" as a copy-paste, simply dropping the code from the included file into the position of the include call. As such, file paths within the included file will be relative to the originally requested file, unless you specify absolute paths, using something like
__DIR__
.See Magic Constants in the manual for details.