如何根据同一类中的另一个常量定义一个类常量?
class A{
const BASE_URL = 'http://example.org'
const API_URL = BASE_URL . '/api'; // < error
}
只能用常量值初始化类常量。你过关了 必须在运行时评估的语句,而类 常量是在更早的编译时定义的。
所以。这不可能。 检查这个不正确的错误报告。
更新:
这是可能的,因为
php-5.6
:
class A {
const BASE_URL = 'http://example.org';
const API_URL = self::BASE_URL . '/api';
}
echo echo A::API_URL; // http://example.org/api