If you do not use an autoloader (see Autoloading CLasses), you will have to manually include all necessary files in all dependent files. It is typically recommended that all files manage all of their own dependencies.
If I understand your dependencies, this would look like:
-
Index.php will need to
require_once
FactoryA.php
-
FactoryA.php will
require_once
A.Class.php & Log.php
-
A.Class.php will
require_once
Database.php & Log.php
-
Database.php will
require_once
Log.php
An alternative method would be to use __autoload
and let it fetch the needed files, as needed, for you. Thus, in your
function __autoload($class_name) {
include './class/' . $class_name . '.php';
}
$a = new A();
However, spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future. See the PHP Ref.
We suggest you check out the PSR-0 standard and you can probably just use the SplClassLoader.php gist.
You will need to rename your classes to follow namespace and path standards, but then you can just do something like:
//This is the only file you need to require
require_once('/path/to/SplClassLoader.php');
$classLoader = new SplClassLoader('Class', './class');
$classLoader->register();
$a = new A();
I have written a small CLI reference project to demonstrate, checkout bubba-h57/AutoLoading and run it from the command line. You should see results like:
[cinamon-vm] AutoLoading> php index.php
From the factory!
From the AClass!
From the Database!
Pure win!
See Why doesn't PHP's Autoload feature work in CLI mode? for a discussion on using the autoloader from command line (cli) scripts.