[英]how to get the constant inside a php class without using reflection
Is that possible to get the constant that is defined inside a class without using the reflection class? 是否可以在不使用反射类的情况下获取在类中定义的常量?
I want something like this: 我想要这样的东西:
<?php
class Test {
const URL = 'https://www.example.com';
public function get($constant)
{
return $constant;
}
}
$test = new Test();
$test->get('URL');
I want the output to be the value of the URL
, which is ' https://www.example.com '. 我希望输出是URL
的值,即“ https://www.example.com ”。 However, right now it just return the actual word 'URL'. 但是,现在它只返回实际的“URL”字样。
Another way: 其他方式:
class Test {
const URL = 'https://www.example.com';
public function get($constant)
{
return constant('Test::'.$constant);
}
}
$test = new Test();
echo $test->get('URL');
Of course it does, you are simply returning the Input back to the user. 当然,它只是将Input返回给用户。 If you want to access a constant via its name do the following: 如果要通过其名称访问常量,请执行以下操作:
<?php
class Test {
const URL = 'https://www.example.com';
}
echo Test::URL;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.