【问题标题】:PHP Autoloading in Namespaces命名空间中的 PHP 自动加载
【发布时间】:2011-04-08 05:20:46
【问题描述】:

我在命名空间中自动加载时遇到了一点问题。如此处的 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() 时,它不会传递该自动加载函数,但在使用 new 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);

【问题讨论】:

  • 重新标记为 php5.3,因为这是一个 5.3-only 问题。

标签: php namespaces autoload spl-autoload-register


【解决方案1】:

这是唯一正确的答案。

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

这里有一些 live 代码可以正常工作!

在../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.

所以我知道它有效。

【讨论】:

  • 似乎可以工作,但仍然无法加载我的功能。我开始认为 PHP 仍然使用它的运行时绑定来覆盖这个功能......这有点糟糕。
  • 是的,它只在使用 new 关键字时进入 spl_autoload .......grrr 为什么 PHP 手册说我可以自动加载函数,如果我不能,可能不得不把它交给他们.
  • 是的,我仍然认为,该死的能够加载依赖于命名空间解析到目录结构的函数会很好。我可能会建议他们,谢谢你的帮助:)。
【解决方案2】:

对 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找到。

上面的代码示例(以及另一个从多个命名空间自动加载的示例)可以在Example Implementations of PSR-4(或GitHub:fig-standards/accepted/PSR-4-autoloader-examples.md)找到。

【讨论】:

  • 实际上现在 PHP7 确实允许直接在命名空间中自动加载函数和方法
  • 嗨@Sammaye,感谢您的提示。你手头有更多信息的链接吗?我似乎找不到任何关于这种变化的信息。
  • 我不知道@Sammaye 从哪里得到的。在 7.2.28 中尝试过,但不起作用。自动加载仅由类触发。我已经知道了,它在 PHP 文档中,但证据胜过 ipse dixit =)
【解决方案3】:

使用Composer 自动加载您的PHP 类。

在我最近的博客文章中查看如何做到这一点:https://enchanterio.github.io/enterprise-level-php/2017/12/25/the-magic-behind-autoloading-php-files-using-composer.html

【讨论】:

  • 之所以现在可以使用是因为 php 现在允许自动加载命名空间函数
  • 当然。 7年后很多事情都变了哈哈。当我自己从 Google 提出这个问题时,我想在这里提及新的处理方式。
  • @boctulus thx 让我知道。几周前我暂停了网站,至少我刚刚部署到 github 页面,所以你可以查看文章,更新答案中的链接。
猜你喜欢
  • 2013-11-25
  • 2014-04-13
  • 2011-08-06
  • 2015-07-15
  • 2012-05-21
相关资源
最近更新 更多