【发布时间】:2023-03-20 17:31:02
【问题描述】:
我希望如果我要在单独的命名空间中定义常量,例如:
namespace config\database\mysql;
const HOST = 'localhost';
const USER = 'testusr';
const PASSWORD = 'testpwd';
const NAME = 'testdb';
我将能够使用__autoload 自动包含它们:
function __autoload($className)
{
echo "Autoload: {$className}\n";
$class_file = str_replace('\\', '/', $className) . ".php";
if(file_exists($class_file)) {
include $class_file;
}
}
echo config\database\mysql\HOST;
但是,这不起作用。 __autoload 没有像类一样调用常量,这给我留下了 Undefined constant 错误。
我可以通过某种方式模拟__autoload 类的常量吗?
【问题讨论】: