douyu8187 2013-04-09 21:26
浏览 31
已采纳

PSR Autoloader功能失败

I think I may be missing something simple here and need a second pair of eyes. This fails with a fatal error class not found. The autoload function was taken from the PSR-0 github page.

<?php

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

    return $fileName;
}

spl_autoload_register('my_autoload');

new Vendor\Package\Example();

This is my index.php, the Class is located at Vendor/Package/Example.php, here is the contents.

<?php

namespace Vendor\Package;

class Example {

    public function __construct() {
        echo __CLASS__ . ' Created with Namespace ' . __NAMESPACE__;
    }

}

It works when I do this require_once my_autoload('Vendor\Package\Example');

  • 写回答

1条回答 默认 最新

  • douzhuangna6906 2013-04-09 21:30
    关注

    The autoload function was taken from the PSR-0 github page.

    No, on the page there is no return $fileName; but require $fileName;

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了