Geoff L
Geoff L

Reputation: 805

Autoloading classes with namespaces and directories on Linux

Hey I'm just learning how namespaces and autoloading works. Can't seem to get it to work could use some help. I use codeigniter and the relevant directory structure is this:

root/
    application/
        classes/
            testa/
                TestClass.php
            testb/
                UserClass.php
    html/
        index.php

the contents of TestClass.php is

namespace application\classes\testa;

class TestClass {
    public $var = 'a default value';
    public function displayVar() {
        echo $this->var;
    }
}

I try to autoload the contents in such a way that it will work with any directory structure inside the classes folder like this inside index.php (I found this code online)

// autoload classes based on a 1:1 mapping from namespace to directory 
// structure.
spl_autoload_register(function ($className) {
    # Usually I would just concatenate directly to $file variable 
    # below
    # this is just for easy viewing on Stack Overflow)

    $ds = DIRECTORY_SEPARATOR;
    $dir = __DIR__;

    // replace namespace separator with directory separator (prolly
    // not required)
    $className = str_replace('\\', $ds, $className);

    // get full name of file containing the required class
    $file = "{$dir}{$ds}{$className}.php";

    // get file if it is readable
    if (is_readable($file)) require_once $file;
});

But when i try to instantiate a class inside a codeigniter view with

$obj = new application\classes\testa\TestClass();

I get

An uncaught Exception was encountered
Type: Error
Message: Class 'application\classes\testa\TestClass' not found

What am I doing wrong? I've never really used namespaces or classes before so I'm kind of lost. I'm thinking the autoloader is written wrong (maybe its for a Windows machine?) but I'm not sure. Thanks for your help.

Upvotes: 1

Views: 352

Answers (2)

Geoff L
Geoff L

Reputation: 805

Thanks for your help. I managed to figure it out. I went with this for the autoloader, which gives the correct path to the classes dir

spl_autoload_register(function ($className) {
    // $ds = '/';
    $ds = DIRECTORY_SEPARATOR;
    // $dir = the path to this file (index.php), it always gives 
    // path to the current file
    $dir = __DIR__ ;
    // replace namespace separator with directory separator (prolly
    // not required)
    $className = str_replace('\\', $ds, $className);
    // get full name of file containing the required class 
    $file = '../' . $className . ".php";
    // get file if it is readable
    if (is_readable($file)) require_once $file;
});

BTW I'm on Linux, not windows

Upvotes: 0

Daniel1147
Daniel1147

Reputation: 151

Since you're running on a windows machine, i assume root to be under C:

In index.php, $file = "{$dir}{$ds}{$className}.php"; will be

C:\root\html\application\classes\testa\TestClass.php

You can't find your TestClass.php there.

The correct file path should be

$file = "{$dir}{$ds}..{$ds}{$className}.php";

which will be

C:\root\html\..\application\classes\testa\TestClass.php

The ..\ means the parent directory.

Upvotes: 1

Related Questions