Alex Rivera | Logout

dynamic class property $$value in php

Asked 2009-05-12T18:45:12.503
42

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?

Edit
Report

1 Answer

19

As the others have mentioned, $this->$string should do the trick.

However, this

$this->$$string;

will actually evaluate string, and evaluate again the result of that.

$foo = 'bar';
$bar = 'foobar';
echo $$foo; //-> $'bar' -> 'foobar'
answered 2009-05-12T18:55:14.543

Your Answer