命名空间中的 PHP 自动加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3642282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Autoloading in Namespaces
提问by Sammaye
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.phpyou should be able to autoload namespace functions with a full qualified name e.g. \glue\common\is_email().
我的命名空间中的自动加载有一个小问题。如此处的 PHP 手册所示:http: //us.php.net/manual/en/language.namespaces.rules.php您应该能够使用完全限定名称自动加载命名空间函数,例如 \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 :.
事情是我有一个函数 spl_autoload_register(array($import, "load")); 在初始命名空间内,但是每当我尝试从初始命名空间调用 \glue\common\is_email() 时,它都不会传递该自动加载函数,但是当使用 new is_email() (在类的上下文中)时,它会传递。我不明白手册说我可以从完全限定名称自动加载,但我不能:。
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);
回答by Theodore R. Smith
Here's the only right answer.
这是唯一正确的答案。
Every namespace needs its own spl_autoload_register() function.
每个命名空间都需要自己的 spl_autoload_register() 函数。
also, spl_autoload_register() syntax changedin 5.3:
此外,spl_autoload_register() 语法在 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 livecode that Just works!
这是一些可以正常工作的实时代码!
in ../WebPageConsolidator.inc.php:
在 ../WebPageConsolidator.inc.php 中:
class WebPageConsolidator
{
public function __construct() { echo "PHP 5.2 constructor.\n"; }
}
in test.php:
在 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.
所以我知道它有效。
回答by Lukas Lukac
Use Composerto autoload your PHP Classes.
使用Composer自动加载您的 PHP 类。
Check out how to do it in my recent blog post: https://enchanterio.github.io/enterprise-level-php/2017/12/25/the-magic-behind-autoloading-php-files-using-composer.html
在我最近的博客文章中查看如何做到这一点:https: //enchanterio.github.io/enterprise-level-php/2017/12/25/the-magic-behind-autoloading-php-files-using-composer。 html
回答by Jpsy
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.
对 OP 问题的误解可能是函数/方法会受到自动加载的影响——而事实并非如此。自动加载仅通过引用类触发。
This being said there still remains the question about autoloading classes in namespaces:
话虽如此,仍然存在关于在命名空间中自动加载类的问题:
As of 2017 the current PHP-FIGstandard for autoloading is PSR-4 which provides the following autoloader code for namespaced classes:
截至 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;
}
});
The full spec text can be found at PSR-4: Autoloader.
完整的规范文本可以在PSR-4: Autoloader 中找到。
The code example above (and another one to autoload from multiplenamespaces) can be found at Example Implementations of PSR-4(or GitHub: fig-standards/accepted/PSR-4-autoloader-examples.md).
上面的代码示例(以及另一个从多个命名空间自动加载的代码示例)可以在PSR-4 的示例实现(或 GitHub:fig-standards/accepted/PSR-4-autoloader-examples.md)中找到。