Tuesday, 17 September 2013

Get object value with variable in PHP

Get object value with variable in PHP

I am trying to get an object value using a variable passed to a function.
Here is a simple example of what I am trying to do:
function get_date($object, $date_name)
{
// this should execute like "$project->date_of_project"
return formatted_date($object->$date_name);
}
echo get_date($project, 'date_of_project');
I am getting the error:
Trying to get property of non-object
I read in a few places to try the following (but it does not work):
$object->{$date_name}[0]
A var_dump shows the property is there:
object(stdClass)#33 (1) {
["date_of_project"]=>
string(19) "2013-04-25 00:00:00"
}

No comments:

Post a Comment