Autoloading
class Directory
Put class files in the class
directory of your package in a PSR-4
style. So \Taeluf\FunnyStrings
should be at class/Taeluf/FunnyStrings.php
Manual
Add Directories to the autoloader:
$dir = __DIR__;
$lia->autoload($dir.'/hierarchy');
$lia->autoload($dir.'/flat',['Payment\\Processor', 'User\\Auth'])
This will allow all the classes below to be autoloaded:
/hierarchy
- Contact/
- Submitter.php: namespace Contact, class Submitter
- Customer/
- Order.php, namespace Customer, class Order
- Person.php, ns Customer, cls Person
/flat
- Visa.php: ns Payment\Processor, cls Visa
- Bitcoin.php: ns Payment\Processor, cls Bitcoin
- Oauth.php: ns User\Auth, cls Oauth
TODO
- Replace PSR-4 style with classmap-style (just scan the directory for all classes & cache the result in a file.)
Add directories to scan
Call $lia->autoload($directory, $baseNamespaces=[])
. This adds the directory to be searched in a PSR-4
style. If you want to do away with a directory, pass namespaces. Like: $lia->autoload($dir, ['Taeluf'])
, then you could place class Taeluf\FunnyStrings
directly in $dir/FunnyStrings.php
& it will be loaded.
If you're developing an app, use the api. Instead of $lia->autoload(...)
, call $lia->callApi('lia.autoload', $directory, $namespaces=[])
; If you're developing a website, $lia->autoload()
is recommended.
Add a custom autoloader (functional)
Call $lia->addAutoloader($callback)
where $callback
takes a single param $fullyQualifiedClassName
. For consistent apis use $lia->api('lia.autoload', 'add', $callback)
instead. Of course, you could just use spl_autoload_register
.
Add a custom autoloader (prefixing)
In one of your components, prefix a function with autoload
(or autoload_
). Something like:
class MyCompo extends \Lia\Compo {
public function autoloadMyOwnWildClasses($className){
//you should check if the file exists, as autoloaders aren't supposed to output errors
$this->myWildMapToFiles[$className];
}
}