Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

spl_autoloader not loading any classes

so i've started using namespaces and read some docs but I seem to be doing something wrong.

First off is my application structure which is build like this:

root
-dashboard(this is where i want to use the autoloader)
-index.php
--config(includes the autoloader)
--WePack(package)
---src(includes all my classes)

now in the src directory I included the classes with:

namespace WePack\src;
class Someclass(){

}

the content of config.php is:

<?php
// Start de sessie
ob_start();
session_start();

// Locate application path
define('ROOT', dirname(dirname(__FILE__)));
set_include_path(ROOT);
spl_autoload_extensions(".php"); // comma-separated list
spl_autoload_register();
echo get_include_path();

and I use it like this in my index.php

require_once ('config/config.php');
use WePack\src;
$someclass = new Someclass;

this is what the echo get_include_path(); returns:

/home/wepack/public_html/dashboard

which is what I want I guess. but the classes are not loaded and nothing is happening. I'm obviously missing something but I can't seem to figure it out. could you guys take a look at it and explain to me why this isn't working?

Upvotes: 0

Views: 433

Answers (2)

Raphael M&#252;ller
Raphael M&#252;ller

Reputation: 2200

The problem here is, that you don't register a callback function with spl_autoload_register(). have a look at the official docs.

To be more flexible, you can write your own class to register and autoload classes like this:

class Autoloader
{
    private $baseDir = null;

    private function __construct($baseDir = null)
    {
        if ($baseDir === null) {
            $this->baseDir = dirname(__FILE__);
        } else {
            $this->baseDir = rtrim($baseDir, '');
        }
    }

    public static function register($baseDir = null)
    {
        //create an instance of the autoloader
        $loader = new self($baseDir);

        //register your own autoloader, which is contained in this class
        spl_autoload_register(array($loader, 'autoload'));

        return $loader;
    }

    private function autoload($class)
    {
        if ($class[0] === '\\') {
            $class = substr($class, 1);
        }

        //if you want you can check if the autoloader is responsible for a specific namespace
        if (strpos($class, 'yourNameSpace') !== 0) {
            return;
        }

        //replace backslashes from the namespace with a normal directory separator
        $file = sprintf('%s/%s.php', $this->baseDir, str_replace('\\', DIRECTORY_SEPARATOR, $class));

        //include your file
        if (is_file($file)) {
            require_once($file);
        }
    }
}

after this you'll register your autoloader like this:

Autoloader::register("/your/path/to/your/libraries");

Upvotes: 4

SuperDJ
SuperDJ

Reputation: 7661

Isn't this what you mean:

spl_autoload_register(function( $class ) {
    include_once ROOT.'/classes/'.$class.'.php';
});

That way you can just call a class like:

$user = new User(); // And loads it from "ROOT"/classes/User.php

Upvotes: 0

Related Questions