加载类时出现spl_autoload_register问题 [英] spl_autoload_register issue while loading class

查看:116
本文介绍了加载类时出现spl_autoload_register问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我早些时候已经在这里问了这个问题,但是提供的解决方案对我不起作用. 这是我的设置:

So I already asked this question here earlier, but the solutions provided didn't work for me. Here's my setup:

/mylib
    /Vendor/Module/MyClass.php
/document_root
    index.php

这是我的index.php

Here's my index.php

<?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, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        echo 'does it come here? nope.'; 
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= BASEPATH.'/'.str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';

    require $fileName; 
}

spl_autoload_register('autoLoader');

//$obj = new MyClass();

$obj = new \Vendor\Module\MyClass();

$obj::test();

 ?>

这是我的MyClass.php

Here's my MyClass.php

<?php

namespace Vendor\Module;

class MyClass{

    public function __construct(){
        echo 'weird';
    }

    public function test(){
        echo 'strange';
    }
}
?>

我的回声均未显示任何内容.显然,我的课程也没有加载.相反,我收到此错误.

None of my echo's display anything. Obviously my class is also not loaded. Instead I get this error.

Fatal error: Call to undefined method MyClass::test() in /<documentroot>/index.php on line 29

请帮助.我已经在这个问题上停留了很长一段时间,而我的其余开发工作仍在遭受痛苦.我尝试移动到spl_autoload_register()只是因为这是推荐的方法.现在,失去的时间让我后悔.

Please help. I've been stuck on this for quite a while now and the rest of my development is suffering. I tried moving to spl_autoload_register() only because it's the recommended way. Now the lost time is making me regret it.

推荐答案

F ** k是.是的,我可以自由地在公开论坛上宣誓.

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

@Shivan Raptor向他们大喊大叫,以帮助我一路走好,没有放弃.

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

自动加载器功能存在许多小问题.但是调试花了我这么长时间,原因很简单,以至于我看不到任何回显消息.只有上帝和XAMPP知道原因.好像XAMPP在第一次运行时以某种方式缓存了该类,后来没有任何更改显示任何效果.但是突然创建一个新的类和类文件开始显示我所有的回声,包括自动加载中的回声.从下面的链接中获取自动加载程序代码的任何人,请确保您查看所有变量的值.如果您没有将所有内容都保留在文档根目录中,那么它开箱即用"是行不通的.而且,如果您不熟悉PSR-0和自动加载的概念,那么这可能会杀死至少一部分功能完善的脑细胞.

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

这是对我有用的最终index.php.

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();
?>

这篇关于加载类时出现spl_autoload_register问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆