开发者

Access property value as string literal in PHP

开发者 https://www.devze.com 2023-02-03 03:14 出处:网络
Okay, im relearning php for a small home project and run into a problem so heres a quick one for all u php experts:

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. Pl开发者_开发问答ease help me here.


$propertyName = 'id';

print($phpObject->{$propertyName});


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号