Daniel A. White Daniel A. White - 1 year ago 166
PHP Question

Get PHP class property by string

How do I get a property in a PHP based on a string? I'll call it

. So what is

$obj->Name = 'something';
$get = $obj->Name;

would be like...

magic($obj, 'Name', 'something');
$get = magic($obj, 'Name');

Answer Source

Like this


$prop = 'Name';

echo $obj->$prop;

Or, if you have control over the class, implement the ArrayAccess interface and just do this

echo $obj['Name'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download