Getting a variable from an object with a string in PHP
I have a Persons
object. It has a variable name
accessible by saying
$p = new Person('John');
echo $p->name;
Now I have a string.
$name = 'name';
I need to get $p->name
using $p
and $name
. Something like
echo $p->[$name];开发者_运维问答
echo $p->{$name};
echo $p->$name;
echo $p->$name; may generates error if it contains special characters so following one is perfect
echo $p->{$name};
精彩评论