11

Okay, im relearning php for a small home project and run into a problem so heres a quick one for all u php experts:

I have build an abstract class which should access properties of YQL Yahoo returned JSON objects decoded to PHP objects. Lets say I want to access the property id then I do like this right:

print($phpObject->id);  // Okay

But I want to be able to access the property in a more abstract manner, ie something like this:

$propertyName = 'id';
print($phpObject[$propertyName]); 
print($phpObject["id"]);    

But none of the above is working - I am sure for obvious reasons, but me not beeing PHP expert I am having a hard time figurring out this call. Please help me here.

Muleskinner
  • 14,150
  • 19
  • 58
  • 79

2 Answers2

25
$propertyName = 'id';

print($phpObject->{$propertyName});
Diablo
  • 3,378
  • 1
  • 22
  • 28
  • 1
    Just as a sidenote - is there not an error in example 2 in this php documentation: http://php.net/manual/en/sdo.sample.getset.php ? – Muleskinner Jan 06 '11 at 22:15
2

You need to use ArrayObject to access it like an array.

Wiseguy
  • 20,522
  • 8
  • 65
  • 81