Long story short: I’m working on redesigning an existing application, with a full rewrite. As a result, I’m forcing myself to stick to strict OOP, and something of a light MVC pattern as well. Since requiring individual files is tedious, I’m looking to integrate some sort of autoload function, using spl_autoload_register().
Code:
class Controller extends SingletonObject {
private $registry;
private $model;
private $view;
private $instance;
private function __construct(){
spl_autoload_register(array($this, "myAutoLoadFunction"));
}
public static function singleton(){
if(!(isset(self::$instance))){
$c = __CLASS__;
$class = new $c();
self::$instance = $class;
}
return self::$instance;
}
public static function myAutoLoader($name){
define(CLASS_PATH, "/var/www/html/portal/dev/");
try {
if(is_readable(CLASS_PATH . "/classes/" . $name . ".php")) {
require_once(CLASS_PATH . "/classes/" . $name . ".php");
} elseif(is_readable(CLASS_PATH . "/interfaces/" . $name . ".php")) {
require_once(CLASS_PATH . "/interfaces/" . $name . ".php");
} elseif(is_readable(CLASS_PATH . "/pages/" . $name . ".php")) {
require_once(CLASS_PATH . "/pages/" . $name . ".php");
} else {
throw new Exception("The file you are attempting to access does not exist.");
}
} catch (Exception $exc) {
echo $exc->getTraceAsString();
}
}
}
Unfortunately, I keep getting the following error wherever I try to instantiate the controller:
Fatal error: Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static method' in /var/www/html/portal/dev/classes/Controller.php:15 Stack trace: #0 /var/www/html/portal/dev/classes/Controller.php(15): spl_autoload_register(Array) #1 /var/www/html/portal/dev/classes/Controller.php(21): Controller->__construct() #2 /var/www/html/portal/dev/classes/Session.php(82): Controller::singleton() #3 /var/www/html/portal/dev/pages/TestSession.php(7): Session->dumpSession() #4 {main} thrown in /var/www/html/portal/dev/classes/Controller.php on line 15
Does anyone have any suggestions on registering an autoload function that doesn’t require sticking a function floating out in a file somewhere? This one is stumping me.