11

名前空間での自動読み込みで少し問題が発生しました。ここのPHPマニュアルに示されているように:http://us.php.net/manual/en/language.namespaces.rules.php完全修飾名で名前空間関数を自動ロードできるはずです(例:\ glue \ common \ is_email() )。

関数spl_autoload_register(array($ import、 "load"));があります。初期名前空間内ですが、初期名前空間から\ glue \ common \ is_email()を呼び出そうとすると、その自動ロード関数は渡されませんが、新しいis_email()(クラスのコンテキストで)を使用すると渡されます。完全修飾名から自動ロードできるとマニュアルに記載されていませんが、できません:。

これが私のコードです:

namespace glue;

require_once 'import.php';

use glue\import as import;
use glue\core\router as router;

$import = new import();

spl_autoload_register(array($import, "load"));

/** Works and echos glue\router **/
$router = new router();

/** Don't do nothing **/
$cheese = \glue\common\is_email($email);

私もこのコードを試しました:

namespace glue;

require_once 'import.php';

use glue\import as import;
use glue\core\router as router;
use glue\common;

$import = new import();

spl_autoload_register(array($import, "load"));

/** Works and echos glue\router **/
$router = new router();

/** Don't do nothing **/
$cheese = common\is_email($email);

そして最後にこのコード:

namespace glue;

require_once 'import.php';

use glue\import as import;
use glue\core\router as router;
use glue\common\is_email as F;

$import = new import();

spl_autoload_register(array($import, "load"));

/** Works and echos glue\router **/
$router = new router();

/** Don't do nothing **/
$cheese = F($email);
4

3 に答える 3

16

これが唯一の正しい答えです。

すべての名前空間には、独自のspl_autoload_register()関数が必要です。

また、spl_autoload_register()構文は5.3で変更されました。

spl_autoload_register(__NAMESPACE__ . "\\className::functionName"));

以下が機能するはずです。

namespace glue;

require_once 'import.php';

use glue\import as import;
use glue\core\router as router;

$import = new import();

spl_autoload_register(__NAMESPACE__ . "\\$import::load"));

/** Works and echos glue\router **/
$router = new router();

/** Don't do nothing **/
$cheese = \glue\common\is_email($email);

これがちょうど機能するいくつかのライブコードです!

../WebPageConsolidator.inc.php:

class WebPageConsolidator
{
    public function __construct() { echo "PHP 5.2 constructor.\n"; }
}

test.phpで:

<?php

namespace WebPage;

class MyAutoloader
{
    public static function load($className)
    {
        require '../' . __NAMESPACE__ . $className . '.inc.php';
    }
}

spl_autoload_register(__NAMESPACE__ . "\\MyAutoloader::load");

class Consolidator extends \WebpageConsolidator
{
    public function __construct()
    {
        echo "PHP 5.3 constructor.\n";

        parent::__construct();
    }
}

// Output: 
// PHP 5.3 constructor.
// PHP 5.2 constructor.

だから私はそれがうまくいくことを知っています。

于 2010-09-04T13:16:40.863 に答える
1

OPの問題における誤解は、おそらく関数/メソッドが自動ロードの対象となるというものですが、そうではありません。自動読み込みは、クラスを参照することによってのみトリガーされます。

これは、名前空間でのクラスの自動ロードに関する質問がまだ残っていると言われています。

2017年現在、オートローディングの現在のPHP-FIG標準はPSR-4であり、名前空間クラスに次のオートローダーコードを提供します。

<?php
/**
 * An example of a project-specific implementation.
 *
 * After registering this autoload function with SPL, the following line
 * would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
 * from /path/to/project/src/Baz/Qux.php:
 *
 *      new \Foo\Bar\Baz\Qux;
 *
 * @param string $class The fully-qualified class name.
 * @return void
 */
spl_autoload_register(function ($class) {

    // project-specific namespace prefix
    $prefix = 'Foo\\Bar\\';

    // base directory for the namespace prefix
    $base_dir = __DIR__ . '/src/';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }

    // get the relative class name
    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    // if the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

完全な仕様テキストは、PSR-4:Autoloaderにあります。

上記のコード例(および複数の名前空間から自動ロードする別のコード例)は、PSR-4の実装例(またはGitHub:fig-standards / accepted / PSR-4-autoloader-examples.md)にあります。

于 2017-05-11T14:29:38.833 に答える
0

Composerを使用してPHPクラスを自動ロードします。

最近のブログ投稿でそれを行う方法を確認してください:https://enchanterio.github.io/enterprise-level-php/2017/12/25/the-magic-behind-autoloading-php-files-using-composer。 html

于 2017-12-26T15:49:27.493 に答える