php后期静态绑定:使用反射返回static::CONST
可能重复:php后期静态绑定:使用反射返回static::CONST,php,reflection,Php,Reflection,可能重复: 我想使用反射发送一个常量数组作为静态调用的结果 class ArrowType extends AbstractAttributeType { const NORMAL = 'normal'; const INV = 'inv'; static function getPossibleValues() { $refl = new \ReflectionClass(__CLASS__); $class_vars = $
我想使用反射发送一个常量数组作为静态调用的结果
class ArrowType extends AbstractAttributeType
{
const NORMAL = 'normal';
const INV = 'inv';
static function getPossibleValues()
{
$refl = new \ReflectionClass(__CLASS__);
$class_vars = $refl->getConstants();
$res = array();
foreach ($class_vars as $name => $value) {
$res[] = static::$$name;
}
return $res;
}
}
这给了我
致命错误:访问未声明的静态属性:ArrowType:$NORMAL
我想去
箭头类型::正常
调用$arrowType->getPossibleValues()时
编辑
为了让我的问题更容易理解,我会告诉你细节。
我正在使用,我正在努力使我的生活更轻松。目前,为了满足库的要求,我必须生成下面的代码
class ArrowType extends AbstractAttributeType
{
const NORMAL = 'normal';
const INV = 'inv';
const DOT = 'dot';
const INVDOT = 'invdot';
const ODOT = 'odot';
const INVODOT = 'invodot';
const NONE = 'none';
static function getPossibleValues()
{
return array(
static::NORMAL,
static::INV,
static::DOT,
static::INVDOT,
static::ODOT,
static::INVODOT,
static::NONE
);
}
static function getReadables()
{
return array(
static::NORMAL => 'normal',
static::INV => 'inv',
static::DOT => 'dot',
static::INVDOT => 'invdot',
static::ODOT => 'odot',
static::INVODOT => 'invodot',
static::NONE => 'none'
);
}
}
我现在试图实现的是在这两个函数中动态构建数组,以便将这些函数放在父类中,并在我的类中声明const部分。我希望这能帮助善良的读者理解我想要什么。等等,等等,等一下
$class_vars = $refl->getConstants();
$res = array();
foreach ($class_vars as $name => $value) {
...
您已经拥有了该值。就在那里。你甚至不需要这辆车。只要
return$refl->getConstants()
虽然这是最终的解决方案,但我不确定它是否足够接近,足以成为一个复制品。当然,我们还要考虑到问题本身的答案:$res[]=$value
或$res=array_值($refl->getConstants())代码>-当使用反射时,它已经存在了。哈哈,我刚刚意识到。是的,这个问题不是很清楚。也许Mylen可以解释一下真正的问题是什么?真正有趣的是,我的帖子被篡改了,标题被更改了!我的问题是关于惰性静态绑定!对你来说可能还不够清楚,但是如果你执行上面的代码,你会得到一个错误,我想要的是在最后一段的blockquote中解释的。你可以问自己,如果我已经有了答案,为什么我会抽出10分钟问一个问题。。我真的不喜欢stackoverflow允许人们在不询问作者的情况下修改你的帖子…@Mylen,请参阅。协作编辑是一项核心功能。