【发布时间】:2020-04-19 22:11:24
【问题描述】:
我在index.php 中设置了一个非常基本的自动加载器,以在hello.php 中获取一个命名空间类。我的开发环境是Ubuntu 12.04。
我为什么要这样做?我正在尝试坚持使用PSR-1 和PSR-2 coding standard,其中包括:
类名必须在 StudlyCaps 中声明
命名空间为 /Vendor/Class(注意:大写)
以下是我的结构和代码有效 之前 对大写进行更改。
文件夹结构
- web
-- index.php
-- core
--- hello.php
自动加载器
在 index.php 中,我有我的自动加载器:
set_include_path(__DIR__);
spl_autoload_extensions('.php,.class.php');
spl_autoload_register();
类文件
在核心文件夹中,我有 hello.php
namespace core;
class hello {
public function __construct() {
echo 'Constructed!';
}
}
有效的代码
如果我在 index.php 中运行 $obj = new \core\hello();,我会返回“已构建!”。太好了!
不起作用的东西
将我的核心文件夹重命名为“Core” - 请注意大写 C,以及 hello.php 中的命名空间@ 到 namespace Core;。
现在让我们用$obj = new \Core\hello();再试一次
Fatal error: Class 'Core\hello' not found in ...
那么请告诉我,为什么我不能使用大写字母来符合 PSR 标准?我做错了什么?
【问题讨论】:
-
dirname(__FILE__)是不必要的;你可以使用__DIR__。 -
谢谢!我会修改...
标签: php