Django Anonymous
Django Anonymous

Reputation: 3025

__autoload with namespace class gives fatal error for file not found

I was going through the __autoload feature that PHP provides.

However this is the index.php I have:-

define('app_path', realpath('../'));

$paths = array(
        app_path, get_include_path());
set_include_path(implode(PATH_SEPARATOR, $paths));

function __autoload($classname)
{
    $filename = str_replace('\\', '/', $classname.'.php');
    require_once $filename;
}

use \engine\controllers as Controllers;

$data = new Controllers\base(); // This one is line no. 25 (Here is error)
echo $data->mind('Hi');

And this one as my base.php:-

 namespace controllers;
 class base {
    public function __construct() {
        echo __CLASS__ . '<br/>';
        echo __NAMESPACE__;
    }

    public function mind($myname)
    {
        echo $myname;
    }
 }

and throws this error:

enter image description here

My directory structure is as follows:

app -> engine -> controller -> base.php

app -> index.php

I am not sure whats wrong is happening. I am just learning how to use namespace and __autoload

I also tried spl_autoload_register but did not success. Kindly suggest.

EDIT:1

Also, if i want to replace it with spl_autoload_register how that can be implemented.

Upvotes: 0

Views: 1642

Answers (1)

silkfire
silkfire

Reputation: 26033

Not sure, but worth a try:

In base.php, change to namespace engine\controllers; on line 1.

And in index.php, change to use engine\controllers as Controllers; (remove leading backslash) on line 23.

Upvotes: 1

Related Questions