问题描述
我已尝试编写以下代码,但无法弄清楚为什么在具有命名空间的 spl_autoload_register() 中找不到该类?
I have tried to write the following code but cannot figure out why it will not find the class in spl_autoload_register() with a namespace?
我得到的错误是:
警告:require_once(src/test\StringHelper.php):打开失败流:没有那个文件或目录
Autoloader.php 文件:
<?php
spl_autoload_register(function($classname){
require_once "src/$classname.php"; // NOT WORKING DYNAMICALLY
// require_once "src/StringHelper.php"; // WORKING WHEN HARD CODED
});
$stringHelper1 = new test\StringHelper(); // Class with namespace defined
echo $stringHelper1->hello() . PHP_EOL; // returns text
src 文件夹中的 StringHelper.php:
<?php namespace test;
class StringHelper{
function hello(){
echo "hello from string helper";
}
}
如果这有所作为,我也会使用 XAMPP.
I am also using XAMPP if this make a difference.
推荐答案
正如评论中已经指出的,你需要去掉除类名之外的所有内容,如下所示:
As already pointed out in the comments, you'll need to strip everything besides the class name, like so:
$classname = substr($classname, strrpos($classname, "\\") + 1);
在自动加载功能的上下文中:
Within the context of your autoloading function:
spl_autoload_register(function($classname){
$classname = substr($classname, strrpos($classname, "\\") + 1);
require_once "src/{$classname}.php";
});
让我们更进一步,利用自动加载函数总是接收限定命名空间而不是例如相对命名空间这一事实:
Let's take this a step further by making use of the fact that an autoload function always receives the qualified namespace as opposed to, for example, the relative namespace:
<?php
namespace Acme;
$foo = new \Acme\Foo(); // Fully qualified namespace
$foo = new Acme\Foo(); // Qualified namespace
$foo = new Foo(); // Relative namespace
在所有三个实例中,我们的自动加载函数总是以 Acme\Foo
作为参数.考虑到这一点,实现将命名空间和任何子命名空间映射到文件系统路径的自动加载器策略相当容易 - 特别是如果我们包含顶级命名空间(Acme
,在本例中)) 在文件系统层次结构中.
In all three instances, our autoload function is always given Acme\Foo
as argument. With this in mind, it's fairly easy to implement an autoloader strategy that maps a namespace and any sub-namespaces to a file system path - especially if we include the top-level namespace (Acme
, in this case) in the filesystem hierarchy.
例如,鉴于我们的某个项目中的这两个类...
For example, given these two classes within some project of ours...
<?php
namespace Acme;
class Foo {}
<?php
namespace Acme\Bar;
class Bar {}
...在这个文件系统布局中...
...within this file system layout...
my-project
`-- library
`-- Acme
|-- Bar
| `-- Bar.php
`-- Foo.php
...我们可以实现命名空间类与其物理位置之间的简单映射,如下所示:
...we could implement a simple mapping between a namespaced class and its physical location like so:
<?php
namespace Acme;
const LIBRARY_DIR = __DIR__.'/lib'; // Where our classes reside
/**
* Autoload classes within the current namespace
*/
spl_autoload_register(function($qualified_class_name) {
$filepath = str_replace(
'\\', // Replace all namespace separators...
'/', // ...with their file system equivalents
LIBRARY_DIR."/{$qualified_class_name}.php"
);
if (is_file($filepath)) {
require_once $filepath;
}
});
new Foo();
new Bar\Bar();
另请注意,您可以注册多个自动加载函数,例如,处理不同物理位置的不同顶级命名空间.不过,在实际项目中,您可能想要熟悉 Composer 的自动加载机制:
Also note that you can register multiple autoloading functions, for example, to handle different top-level namespaces in different physical locations. In a real-word project, though, you might want to get yourself acquainted with Composer's autoloading mechanism:
- https://getcomposer.org/doc/01-basic-usage.md#autoloading
- https://getcomposer.org/doc/04-schema.md#autoload
- https://getcomposer.org/doc/articles/autoloader-optimization.md
在某些时候,您可能还想查看 PHP 的自动加载规范:
At some point, you might also want to have a look into PHP's autoloading specification:
这篇关于PHP 自动加载类不适用于命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!