spl_autoload_register issue while loading class

妖精的绣舞 提交于 2019-12-05 20:17:35

Your are calling the test() function incorrectly (using static way?).

Call the function with:

$obj = new MyClass();
$obj->test();

If you intend to use static method like MyClass::test(), declare your function in your class as:

public static function test() {
}

Moreover, your autoloader is over-complicated. It can be simplified as:

$class_dir = array(
    '/mylib/Vendor/Module/',
    // Add more paths here ( or you can build your own $class_dir )
);

function class_loader($class_name) {
    global $class_dir;
    foreach ($class_dir as $directory) {
        if (file_exists($directory . $class_name . '.php')) {
            require_once($directory . $class_name . '.php');
            return;
        }
    }
}
spl_autoload_register('class_loader');

3rd edit:

I noticed the path you set is incorrect.

$fileName .= BASEPATH.'/'.str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

It should be:

$filename = BASEPATH .'/' . $filename . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

As said before, it can be easily debugged by echoing the value of $filename.

F**k yes. Yes, I can take the liberty to swear on a public forum this one time.

A huge shout out to @Shivan Raptor for helping me along the way and not giving up.

There were numerous minor issues in the auto-loader function. But the debugging took me so long for just a simple reason that I couldn't see any echo messages. Only lord and XAMPP knows why. Seemed like XAMPP had somehow cached the class on first run or something and no changes later showed any effect. But creating a new class and class file all of a sudden started showing all my echo including the ones inside autoload. Anyone who has picked up the auto-loader code from the link below, please ensure you look at all the variables' values. It doesn't work "out of the box", if you don't keep everything in the document root. And if you are new to both PSR-0 and concept of auto loading, this can kill at least a sizable portion of your perfectly capable brain cells.

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

Here's the final index.php that worked for me.

<?php

define('CLASSDIR', 'mylib');
define('BASEPATH',  @realpath( dirname (__FILE__).'/../').'/'.CLASSDIR);

spl_autoload_register(null, false);
spl_autoload_extensions('.php');

function autoLoader($className){
    $className = ltrim($className, '\\');
    $classPath  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {    
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $classPath = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
    }
    $fileName = BASEPATH.'/'.$classPath.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
    require $fileName;
}

spl_autoload_register('autoLoader');

$obj = new \Vendor\Module\MyClass();
$obj::test();
?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!