So i embarked on the adventure of php namespaces having experience with c++ and java i figured ehh no problem till i attempted to autoload a namespaced class and triggered and error the class was not found knowing all to well the class exists. The class i tried to load was a test version of my config class…Just FYI… I don’t understand where i went wrong in this case so if you could explain a brief why would also be very great full.
So below are my bootstrap and loader class and critique is also welcomed.
Bootstr
define('FW_DIR' , 'Framework');
define('APP_DIR' , 'Application');
define('DS' , '/');
define('EXT' , '.php');
define('VER' , '0.0.1');
define('PHP' , '5.3.2');
define('ERROR' , 0);
define('NOTICE' , 1);
define('PARSE' , 2);
define('WARNING' , 3);
define('ERROR_LOG' , 0);
define('DEBUG_LOG' , 1);
define('DATRA_LOG' , 2);
define('CWD' , str_replace('\\\\' , '/' , getcwd()) . DS);
define('FW' , CWD . FW_DIR . DS);
define('APP' , CWD . APP_DIR . DS);
// attempt to load the laoder class
if(file_exists(FW . 'Loader' . EXT)){
@require FW . 'Loader' . EXT;
}else{
exit('The Loader Class Was Not Located');
}
// set the loader path
\\Framework\\Core\\Loader::setPath('Loader' , FW . 'Loader' . EXT);
// set the config path
\\Framework\\Core\\Loader::setPath('Config' , FW . 'Config' . EXT);
// regsiter the autoloader
\\Framework\\Core\\Loader::autoload();
// try and call dummy class __construct echo "hi";
$config = new \\Framework\\Core\\Config();
namespace Framework\\Core;
class Loader{
// loaded paths and classes
private $_loaded = array();
// registry of paths
private $_paths = array();
// class instance
private static $_instance;
//private __construct so won't be called
private function __construct(){}
// the class instance method protected so only can be called by local methods;
protected static function _instance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self;
}
return self::$_instance;
}
// protected helper method check if class is loaded
protected function _isLoaded($classname){
if(isset($this->_loaded[$classname])){
return TRUE;
}
return FALSE;
}
// helper to set the loaded class
protected function _setloaded($classname , $path){
if(FALSE === $this->_isLoaded($classname)){
$this->_loaded[$classname] = $path;
return TRUE;
}
return FALSE;
}
// helper to get the loaded class
protected function _getLoaded($classname){
if(FALSE !== $this->_isLoaded($classname)){
return $this->_loaded[$classname];
}
return FALSE;
}
// delete the loaded class
protected function _cleanLoaded($classname){
if(FALSE !== $this->_isLoaded($classname)){
unset($this->_loaded[$classname]);
return TRUE;
}
return FALSE;
}
// flush all loaded classes
protected function _flushLoaded(){
return $this->_loaded;
}
// check if path exists ro has been set
protected function _hasPath($classname){
if(isset($this->_paths[$classname])){
return TRUE;
}
return FALSE;
}
// set a class and path to the class
protected function _setPath($classname , $path){
if(FALSE === $this->_hasPath($classname)){
$this->_paths[$classname] = $path;
return TRUE;
}
return FALSE;
}
// get that path if it exists
protected function _getPath($classname){
if(FALSE !== $this->_hasPath($classname)){
return $this->_paths[$classname];
}
return FALSE;
}
// delete the path if it exists
protected function _cleanPath($classname){
if(FALSE !== $this->_hasPath($classname)){
unset($this->_paths[$classname]);
return TRUE;
}
return FALSE;
}
// get all paths
protected function _flushPaths(){
return $this->_paths;
}
// set the path callable
public static function setPath($classname , $path){
return self::_instance()->_setPath($classname , $path);
}
// get the path callable
public static function getPath($classname){
return self::_instance()->_getPath($classname);
}
// delete the path callable
public static function deletePath($classname){
return self::_instance()->cleanPath($classname);
}
// get all paths callable
public static function getAllPaths(){
return self::_instance()->_flushPaths();
}
public static function getAllLoaded(){
return self::_instance()->_flushPaths();
}
// method for loading and autoloading
public static function load($classname){
if(FALSE !== $path = self::getPath(__NAMESPACE__ . $classname)){
require $path;
}
return FALSE;
}
// register the autoload method
public static function autoload(){
return spl_autoload_register('\\Framework\\Core\\Loader::load');
}
}