Autoloading a class once class is called from an array using PHP

dacho

New Member
I have been looking through different frameworks and how they implement autoloading of their classes but I am new to PHP so am unsure whether I am interpreting its functions correctly. I have tried to create my own class only using it to autoload all my classes, the classes cannot be accessed. Here is what my code looks like:\[code\]class Autoloader { private $map = array(); private $directories = array(); public function register() { spl_autoload_register(array($this, 'load')); } public function unregister() { spl_autoload_unregister(array($this, 'load')); } public function map($files) { $this->map = array_merge($this->map, $files); $this->load($this->map); } public function directory($folder) { $this->directories = array_merge($this->directories, $folder); } public function load($class) { if ($file = $this->find($class)) { require $file; } } public function find($file) { foreach ($this->directories as $path) { if (file_exists($path . $file . '.php')) { return $path . $file . '.php'; } } }}\[/code\]I load the classes from my bootstrap file like so\[code\]require('classes/autoload.php');$autoload = new Autoloader();$autoload->map(array( 'Config' => 'classes/config.php', 'Sql' => 'classes/db.php'));$autoload->directory(array( 'classes/'));$autoload->register();\[/code\]Then I attempt to instantiate one of the classes that have been mapped\[code\]$sql = new SQL($dbinfo);$sql->query($query);\[/code\]What is wrong with what I have done and am I doing this correctly? I basically want the autoload class to map an array of classes from the bootstrap file and include the files when they have been called/instantiate and stop including them once they are no longer in use.
 
Back
Top