沃梦达 / 编程问答 / php问题 / 正文

php中的动态类属性$$value

dynamic class property $$value in php(php中的动态类属性$$value)

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

问题描述

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

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
    }
}

评估字符串的正确方法是什么?

what is the correct way to eval the string?

推荐答案

当使用字符串变量引用对象的成员变量时,只需使用一个 $.

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

echo $this->$string;

这篇关于php中的动态类属性$$value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:php中的动态类属性$$value

基础教程推荐