PicoraAutoLoader
Class wrapper around PHP's built in __autoload function.
The AutoLoader class is an object oriented hook into PHP's __autoload functionality.
//single files
PicoraAutoLoader::addFile('PageController','controllers/PageController.php');
//multiple files
PicoraAutoLoader::addFile(array('class'=>'file','class'=>'file'));
//whole folders
PicoraAutoLoader::addFolder('path');
When adding a whole folder each file should contain one class named the same as the file sans ".php" (PageController => PageController.php)
__autoload is defined in the Picora.php file.
Method Overview
Return | Visibility | Name | Parameters |
void | static public | addFolder | (mixed $folder) |
void | static public | addFile | (mixed $class_name, mixed $file) |
Method Detail
static public addFolder()
Parameter Type | Name | Description |
mixed | $folder | string, full path to a folder containing class files, or array of paths. |
PicoraAutoLoader::addFolder('/path/to/my_classes/');
PicoraAutoLoader::addFolder(array('/path/to/my_classes/','/more_classes/over/here/'));
static public addFile()
Parameter Type | Name | Description |
mixed | $class_name | string class name, or array of class name => file path pairs. |
mixed | $file | Full path to the file that contains $class_name. |
PicoraAutoLoader::addFile('Controller','/path/to/Controller.php');
PicoraAutoLoader::addFile(array('Controller'=>'/path/to/Controller.php','View'=>'/path/to/View.php'));
This class triggers the following events, which you can observe with the following syntax:
PicoraEvent::observe('event_name','my_function'); //or
PicoraEvent::observe('event_name',array($my_object,'my_instance_method')); //or
PicoraEvent::observe('event_name',array('MyClass','my_static_method'));
Return | Name | Signature | Description |
bool | PicoraAutoLoader.load | (string class_name) | Callback must return true if a file was included or required. |
Declared in: PicoraAutoLoader.php
final class PicoraAutoLoader {
static protected $files = array();
static protected $folders = array();
/**
* <pre class="highlighted"><code class="php">PicoraAutoLoader::addFile('Controller','/path/to/Controller.php');
* PicoraAutoLoader::addFile(array('Controller'=>'/path/to/Controller.php','View'=>'/path/to/View.php'));</code></pre>
* @param mixed $class_name string class name, or array of class name => file path pairs.
* @param mixed $file Full path to the file that contains $class_name.
* @return void
*/
static public function addFile($class_name,$file = false){
if(!$file && is_array($class_name))
foreach($class_name as $key => $value)
self::addFile($key,$value);
else
self::$files[$class_name] = $file;
}
/**
* <pre class="highlighted"><code class="php">PicoraAutoLoader::addFolder('/path/to/my_classes/');
* PicoraAutoLoader::addFolder(array('/path/to/my_classes/','/more_classes/over/here/'));</code></pre>
* @param mixed $folder string, full path to a folder containing class files, or array of paths.
* @return void
*/
static public function addFolder($folder){
if(is_array($folder))
foreach($folder as $f)
self::addFolder($f);
else
self::$folders[] = $folder;
}
static public function load($class_name){
foreach(self::$files as $name => $file){
if($class_name == $name){
require_once($file);
return true;
}
}
foreach(self::$folders as $folder){
if(substr(0,-1) != DIRECTORY_SEPARATOR)
$folder .= DIRECTORY_SEPARATOR;
if(file_exists($folder.$class_name.'.php')){
require_once($folder.$class_name.'.php');
return true;
}
}
foreach(PicoraEvent::getObserverList('PicoraAutoLoader.load') as $callback)
if(call_user_func($callback,$class_name) === true)
return true;
return false;
}
}