Quote from Autoloading Classes :
Many developers writing object-oriented applications create one PHP source file per class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
In PHP 5, this is no longer necessary. The spl_autoload_register() function registers any number of autoloaders, enabling for classes and interfaces to be automatically loaded if they are currently not defined. By registering autoloaders, PHP is given a last chance to load the class or interface before it fails with an error.
Here comes the question, what if there are multiple classes in a single php file, is it suitable for autoload usage? or do I have to use require filepath
statement?
For example, I have a protocol file under Protobuf\Client.php:
<?php
namespace Protobuf;
class A {
...
}
class B {
...
}
Client.php
. The idea is to translate your namespace\classname into adirectory\filename.php
– BlackingtonA.php
then when you callnew Protobuf\A()
it will find it. Otherwise you will have to create a overly-complex autoloader. – BlackingtonA
class, then you can haveB
on the same file, but only if you have already autoloadedA
otherwise you have to make some algorythm to know thatA
andB
are on the same page. – Blackington