Daily Archives: May 7, 2016

PHP Tip : Access object properties dynamically

If you want to pass a string into a function and use that string as the property of an object to get the value of that property you can use the following syntax.

function getObjectValueDynamically ($property_to_access) {
$value = $object->{$property_to_access};
return $value;
}

The part that you need to note here is, the parameter is surrounded by two Curly brackets like,

$object->{$property_to_access}
Advertisements