动态类属性$$值在php [英] dynamic class property $$value in php

查看:112
本文介绍了动态类属性$$值在php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何引用只知道字符串的类属性?

How can i reference a class property knowing only a string?

class Foo
{
    public $bar;

    public function TestFoobar()
    {
        $this->foobar('bar');
    }

    public function foobar($string)
    {
         echo $this->$$string; //doesn't work
    }
}

to eval the string?

what is the correct way to eval the string?

推荐答案

在使用字符串变量引用对象的成员变量时,只需要使用一个$。 p>

You only need to use one $ when referencing an object's member variable using a string variable.

echo $this->$string;

这篇关于动态类属性$$值在php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆