我有以下测试代码:
namespace Test {
const ONE = 50;
class A {
const TWO = 5;
public function pA($string) {
return $string;
}
}
$a = new A();
print $a->pA($a::TWO);
print "This is a string: {$a->pA($a::TWO)}";
print "This is a namespace constant: " . ONE;
print "This is a namespace constant: " . \Test\ONE;
}
所有这些示例都有效,但这不是我要找的。p>
我可以像前两个例子一样使用字符串组合来表示常量吗?我试过很多组合,比如 "${\Test\B}"
或 "${B}"
或 "${\B}"
但是,到目前为止,运气不好。
也许这是不可能的,我做得太过头了,但无论如何......有没有办法做到这一点?
最佳答案
这是行不通的。您可以在双引号字符串中使用 $variables、函数或对象方法调用,但不能使用常量。引用 PHP string parsing documentation .您会发现许多有用的示例。
关于PHP - 在字符串中打印命名空间常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813415/