【问题标题】:PSR Autoloader function FailingPSR Autoloader 功能失败
【发布时间】:2013-04-09 21:26:29
【问题描述】:

我想我可能在这里遗漏了一些简单的东西,需要第二双眼睛。这将失败,找不到致命错误类。自动加载功能取自 PSR-0 github 页面。

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

这是我的index.php,Class位于Vendor/Package/Example.php,这里是内容。

<?php

namespace Vendor\Package;

class Example {

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

}

当我这样做时它会起作用require_once my_autoload('Vendor\Package\Example');

【问题讨论】:

    标签: php oop psr-0


    【解决方案1】:

    自动加载功能取自 PSR-0 github 页面。

    不,on the page 没有return $fileName;require $fileName;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 2015-12-16
      • 2021-10-05
      • 2018-12-06
      • 2016-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多