【发布时间】:2015-07-21 21:36:31
【问题描述】:
我有两个文件,第一个是 Test.php,看起来像这样:
<?php
namespace My\Namespaces\Test;
class Test {
const ALERT_EMAIL = "AlertEmail";
const ALERT_SMS = "AlertSms";
const ALERT_NOTIFICATION = "AlertNotification";
} // class - EnumAlertType
?>
另一个文件尝试使用 Test.php 中的 const
<?php
use My\Namespaces\Test;
$a = Test::ALERT_SMS;
?>
但我仍然收到Class 'My\Namespaces\Test' not found 错误,我不确定我是否正确使用了命名空间。谢谢
【问题讨论】:
-
我认为这是在第二个示例中打开问题时的拼写错误(
NamespacevsNamespaces),对吧?无论如何,您是否正确配置了自动加载(因为我在您的代码中没有看到任何手动包含的提及)? -
也许你会
include()文件而不是use类。 -
这里的类全名是
My\Namespaces\Test\Test。要获得常数,您必须这样做:My\Namespaces\Test\Test::ALERT_SMS。命名空间就像硬盘上的文件夹,任何定义的类都像文件。My\Namespaces\Test是文件夹,Test是文件。 -
@SverriM.Olsen:命名空间就像类路径的文件夹。因此
My\Namespaces\Test代表一个类,而不是一个文件夹。 See theuse My\Full\Classnamein the PHP doc.
标签: php