Friday, 30 November 2018

php - autoload not working with static method

I using spl_autoload_register to autoload class like

My Structure

index.php
Module\Autoloader.php
Module\MyClass.php
Test\test.php

in index.php file

require_once ("Module\Autoloader.php");
use Module\MyClass;
include 'Test\test.php';

in Module\Autoloader.php file

class Autoloader {
static public function loader($className) {
    $filename = __DIR__."/" . str_replace("\\", '/', $className) . ".php";
    echo $filename.'<br>';
    if (file_exists($filename)) {
        include($filename);
    }
}

} spl_autoload_register('Autoloader::loader');

in Module\MyClass.php file

namespace Module;
class MyClass {
    public static function run() {
        echo 'run';
    }
}

in Test\test.php file

MyClass::run();

But it has error

Fatal error: Uncaught Error: Class 'MyClass' not found in ..\Test\test.php

How to fix that thank



from php - autoload not working with static method

No comments:

Post a Comment