So i have started undertaking a task to build re-usable components. The first one i attempted to write is a Loader and all was going well just fixing some bugs from my test cases. And now i have an error for the life of me i cannot figure out where i went wrong.
All classes listed below.
bootstrap.php
autoloader.phpPHP Code:
/* root path */
define('PATH', str_replace('\\' , '/' , getcwd()) . '/');
/* root path */
define('PATH', str_replace('\\' , '/' , getcwd()) . '/');
/* loader namespace */
use Elements\Loader as Loader;
/* get the autoload class */
require PATH . 'Loader/Autoload.php';
/* attempt to register our firt class*/
$autoloader = new Loader\Autoload;
/* register the class for autoloading
* $autoloader->register('classname' , 'path to teh class' , 'class prefix or namespace') */
$autoloader->register('LoaderTest', PATH . 'Tests/LoaderTest' , 'Elements\Tests');
PHP Code:// the loader namespace
namespace Elements\Loader;
// get the abstract loader
require 'Loader.php';
class Autoload extends Loader{
// the autoload method using loader request method to load the class
public function load($class){
$this->request($class);
}
// register the current autoloader
public function start(){
return \spl_autoload_register(array($this => 'load'));
}
// regsiter class for autoloading
public function register($class, $path , $prefix = ''){
$this->register($prefix.$class, $path);
}
}
abstract loader.php
PHP Code:namespace Elements\Loader;
abstract class Loader{
protected $_classes = array();
abstract protected function load($class);
protected function register(array $classes = array() , $overwrite = FALSE){
if(! \is_array($classes)){
return FALSE;
}
if($overwrite === FALSE){
$matched = \array_intersect_assoc($classes , $this->classes);
foreach($macthed as $key => $value){
unset($classes[$key]);
}
}
$this->_classes = \array_merge($classes, $this->_classes);
return TRUE;
}
protected function request($class , $return_obj = FALSE){
if(!\class_exists($class) or !\interface_exists($class)){
if(isset($this->_classes[$class])){
$class = \str_replace('.php' , '', $this->_classes[$class]);
if(\file_exists(\trim($class, '/'). '.php')){
require \trim($class, '/') . '.php';
}
}
if(\class_exits($class, FALSE) or \interface_exists($class, FALSE)){
if($return_obj === FALSE){
return TRUE;
}
else{
return new $class();
}
}
else{
exit("Loader Exception: could not load the requested class {$class}");
}
}
return TRUE;
}
protected function delete($class){
if(isset($this->_classes[$class])){
unset($this->_classes[$class]);
return TRUE;
}
return FALSE;
}
protected function getClasses(){
return $this->_classes;
}
}



Reply With Quote

Bookmarks