1

ここで単純なものが欠けている可能性があり、2 番目の目が必要になると思います。これは致命的なエラー クラスが見つからないため失敗します。自動ロード機能は、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 です。クラスは 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');

4

1 に答える 1

4

自動ロード機能は、PSR-0 github ページから取得されました。

いいえ、ページにはありませんreturn $fileName;require $fileName;

于 2013-04-09T21:30:03.993 に答える