在自动加载的实现文件中找不到 PHP 类
我真的希望这不是重复的,但我在这里:
我使用 Zend 的自动加载器来加载类。它似乎有效,至少在实例化我的类(Common_TestTest)时加载了正确的文件,该类是在 Common/TestTest.php 中实现的。但随后我收到以下错误消息:
“在 Common/TestTest.php 中找不到类 Common_TestTest。”
TestTest.php 中除了类之外没有任何内容:
<?php
class Common_TestTest extends PHPUnit_Framework_TestCase
{
public function testTesting() {
$this->assertTrue(true);
$this->assertFalse(true);
}
}
我尝试在文件末尾转储 get_declared_classes
,一切看起来都很好,Common_TestTest
是声明的类之一 - 但离开文件时仍然抛出异常。
最有趣的是:当我将类的名称从 Common_TestTest
更改为 TestTest
时,会发生相同的事情 - 只是错误消息将缺少的类的名称声明为 <代码>“测试测试”。所以它肯定能看到班级并对其存在做出反应。
I really hope this isn't a duplicate, but here I go:
I use Zend's autoloader to load classes. It seems to work, at least it loads the correct file when instantiating my class (Common_TestTest) which is implemented in Common/TestTest.php. But then I get the following error message:
"Class Common_TestTest could not be found in Common/TestTest.php."
There's nothing in TestTest.php other than the class:
<?php
class Common_TestTest extends PHPUnit_Framework_TestCase
{
public function testTesting() {
$this->assertTrue(true);
$this->assertFalse(true);
}
}
I tried dumping get_declared_classes
at the end of the file, everything looks fine, Common_TestTest
is one of the declared classes - but the exception is still thrown when leaving the file.
The funniest bit is: When I change the name of the class from Common_TestTest
to TestTest
the same things happens - only that the error message states the name of the missing class as "TestTest"
. So it definitely sees the class and reacts to it's presence.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于导致问题的原因,我想到了两种可能性:
类名和文件名之间存在一些大小写不匹配,例如 Testtest.php
注册命名空间时,您使用“Common”而不是“Common_”。 Zend_Loader_Autoloader 不区分 PHP 5.3 风格的命名空间和 ZF 风格的命名空间(而不是 Prefix)。通过附加下划线,您可以确保您的命名空间被解释为类前缀而不是真正的命名空间。
There are 2 possibilities that come to my mind as to what causes the problem:
You have some case-mismatch between class-name and file-name, e.g. Testtest.php
When registering the Namespace you use "Common" instead of "Common_". Zend_Loader_Autoloader does not distinguish between PHP 5.3 style namespaces and ZF-style namespaces (rather Prefix). By appending the underscore you make sure, that your namespace is interpreted as a class-prefix rather than a real namespace.