命名空间中的 PHP 自动加载

发布于 2024-09-17 00:36:11 字数 1500 浏览 6 评论 0原文

我的命名空间中的自动加载遇到了一些小问题。如 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);

I have had a slight problem with autoloading in my namespace. As shown on the PHP manual here: http://us.php.net/manual/en/language.namespaces.rules.php you should be able to autoload namespace functions with a full qualified name e.g. \glue\common\is_email().

Thing is I have a function spl_autoload_register(array($import, "load")); within the initial namespace but whenever I try and call \glue\common\is_email() from the initial namespace it will not pass that autoload function but when using new is_email() (in the context of a class) it will. I don't get it the manual says I can autoload from fully qualified names but I can't :.

Here's my code:

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

I also tried this code as well:

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

and finally this code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舟遥客 2024-09-24 00:36:18

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

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

漫漫岁月 2024-09-24 00:36:16

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:自动加载器

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

The misconception in the question of the OP is probably that functions/methods would be subject to autoloading – which they are not. Autoloading is only triggered by referencing classes.

This being said there still remains the question about autoloading classes in namespaces:

As of 2017 the current PHP-FIG standard for autoloading is PSR-4 which provides the following autoloader code for namespaced classes:

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

The full spec text can be found at PSR-4: Autoloader.

The code example above (and another one to autoload from multiple namespaces) can be found at Example Implementations of PSR-4 (or GitHub: fig-standards/accepted/PSR-4-autoloader-examples.md).

堇年纸鸢 2024-09-24 00:36:15

这是唯一正确的答案。

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

所以我知道它有效。

Here's the only right answer.

Every namespace needs its own spl_autoload_register() function.

also, spl_autoload_register() syntax changed in 5.3:

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

The following should work:

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

Here is some live code that Just works!

in ../WebPageConsolidator.inc.php:

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

in 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.

So I know it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文