[英]PHP: Getting value of constant of class from string
例子如下:
<?php
class test{
const A = "1";
const B = "2";
public function getStr($a){
echo self::$a;
}
}
$c = new test();
$c->getStr("A");
?>
当我使用 getStr("A") 时,如何在窗口中回显变量“A”
它可以通过ReflectionClass::getConstants
来完成。
<?php
ini_set('display_errors', 1);
class test{
const A = "1";
const B = "2";
public function getStr($a){
$class= new ReflectionClass(self::class);//passing class name to ReflectionClass
echo $class->getConstant($a);//getting required constant.
}
}
$c = new test();
$c->getStr("A");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.