Skip to content
Justin Campo edited this page Oct 24, 2015 · 1 revision

Autoloading allows for the calling of php classes without having to include the file.

Note: The system only allows for autoloading of files that match the directory structure of their namespace (see comments in autoload() method below)

##Usage

$example = /xesm/model/example.php

Code Explanation

amespace xesm\core;
class init
{
    __construct()
    {
        //This commands register the autoloading method so it will be triggered
        //if a call to a class is performed that is not loaded into memory
         //note the autoloader requires the log class
         spl_autoload_register('\xesm\core\init::autoload');
    }
    
    public function autoload($class_request)
    {
        //so this method takes the call (xesm/model/example) and parses it and trys to load the file
        $this->c->log->core("Start autoload : ".$class_request);
        $class_data = explode("\\",$class_request);
        //if you notice the plugin classes do not match the directory structure do we have to add custom coding
        if (isset($class_data[0]) AND $class_data[0] == "plugin"){ $class_data[0] = "xesm/plugin"; }
        $class_file = $this->dir_root.$class_data[0]."/".$class_data[1]."/".$class_data[2].".php";
    
        //now we try to load the file
        if (is_file($class_file)){
            include_once($class_file);
            $this->c->log->core("Autoload Success : ".$class_file);
        } else {
            $this->c->log->core("Autoload Fail : ".$class_file);
            //die("Fatal Autoload : ".$class_name);
        }
    }
}
Clone this wiki locally