根据PHP
页面的置顶评论 spl_autoload_register( )
强>:
Good news for PHP 5.3 users with namespaced classes:
When you create a subfolder structure matching the namespaces of the >containing classes, you will never even have to define an autoloader.
<?php
spl_autoload_extensions(".php"); // comma-separated list
spl_autoload_register();
?>
但是,当我有以下结构时:
* classes/someclass.php
* index.php
其中 someclass.php
包含以下内容:
<?php
class someclass {
function __construct( ) {
echo 'It works!';
}
}
?>
和index.php
包含:
<?php
spl_autoload_extensions(".php");
spl_autoload_register();
new classes\someclass;
?>
然后我得到以下错误:
Fatal error: spl_autoload(): Class classes\someclass could not be loaded
我是不是理解错了?我怎样才能使它工作?
来自评论
这对类(class)也不起作用:
<?php
namespace classes;
class someclass {
function __construct( ) {
echo 'It works!';
}
}
?>
最佳答案
在您的 someclass.php 文件中,您必须在开头定义命名空间。
<?php
namespace classes;
关于php - 根据目录结构自动加载命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30569615/