Question

I am new to namespaces and I guess autoloading in the method of SplClassLoader. I've tried search for many tutorials but not having much luck. Perhaps you guys can help me get this going?

Directory Structure

- Oram
    - Lib
        - Classes
            Test.php
- index.php
- SplClassLoader.php

Test.php

<?php
namespace Oram\Lib\Classes;

class Test
{
    function __construct()
    {
        echo "Test Class loaded";
    }
}

index.php

<?php
require_once('SplClassLoader.php');

$loader = new SplClassLoader('Lib', 'Oram\Lib'); 
$loader->register();

use Oram\Lib\Classes\Test;
$test = new Test();

This is all inside localhost/website/ too btw as I am running it on WAMP.

Fatal error: Class 'Oram\Lib\Classes\Test' not found in C:\Program Files\wamp\www\website\index.php on line 10

Any advice or if someone could point me to some reading resources to get my head around this would be great.

Thank you

No correct solution

OTHER TIPS

Edit: I have it! I think you have to change the backslash (\) to a slash (/).

<?php
    require_once('SplClassLoader.php');

    $loader = new SplClassLoader('Lib', 'Oram/Lib'); 
    $loader->register();

    use Oram\Lib\Classes\Test;
    $test = new Test();
?>
Take a look on [this][1]. 

Try that. I have added a \ before your namespace. That could be the answer:

$loader = new SplClassLoader('Lib', '\Oram\Lib'); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top