直接上代码:
hello.php:
index.php:
namespace mine;
const name = 'index.php/name';
include './hello.php';
echo constant('name'),PHP_EOL; //输出 hello.php/name
echo name; //输出 index.php/name

问题:我在index.php中引入了hello.php,目的是为了测试命名空间。但是最后两种方式访问常量的结果却是不同的,求助一下各位~
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
常量也是受到命名空间影响的, 你的hello.php里面的常量实际上是
\name, index.php 里面的实际上是\mine\name