问题 PHP在命名空间中自动加载


我的命名空间中的自动加载有点问题。如PHP手册所示: http://us.php.net/manual/en/language.namespaces.rules.php 您应该能够使用完全限定名称自动加载命名空间函数,例如\胶\共同\ 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);

4649
2017-09-04 12:31


起源

被重新标记为php5.3,因为这是一个仅限5.3的问题。 - Theodore R. Smith


答案:


这是唯一正确的答案。

每个命名空间都需要自己的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.

所以我知道它有效。


13
2017-09-04 13:16



似乎它可以工作,但它仍然不会加载我的功能。我开始认为PHP仍然使用它的运行时绑定覆盖了这个功能...这有点糟透了。 - Sammaye
是的,当使用new关键字时,它只会进入spl_autoload ........ grrr为什么PHP手册说我可以自动加载功能,如果我不能,可能要把它带给他们。 - Sammaye
Autload 只要 与...合作 类。 - Theodore R. Smith
是的,我仍然认为,该死的能够加载依赖于命名空间解析的功能到目录结构将是很好的。我可能会建议他们,谢谢你的帮助:)。 - Sammaye


使用 作曲家 自动加载你的PHP类。

在我最近的博客文章中查看如何执行此操作: https://enterprise-level-php.com/2017/12/25/the-magic-behind-autoloading-php-files-using-composer.html


2
2017-12-26 15:49



之所以现在这样做是因为php现在允许自动加载命名空间函数 - Sammaye
当然。 7年后,很多事情都改变了哈哈。我想在这里提到一种新的做法,因为我自己从Google获得了这个问题。 - EnchanterIO


OP问题中的误解可能是函数/方法会受到自动加载 - 它们不是。自动加载仅由引用类触发。

据说这仍然存在 关于命名空间中的自动加载类的问题

截至2017年的当前 PHP-图 自动加载的标准是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:自动加载器

上面的代码示例(另一个来自自动加载  命名空间)可以在 PSR-4的示例实现 (或GitHub: 无花果标准/接受/ PSR-4-autoloader-examples.md)。


0
2018-05-11 14:29



实际上现在PHP7确实允许直接在命名空间中自动加载函数和方法 - Sammaye
嗨@Sammaye,谢谢你的暗示。您手头有更多信息吗?我似乎无法找到有关这种变化的任何信息。 - Jpsy