[英]PHP access object values dynamically by variable name
我有这个小片段:
$action = $_GET['action'];
// some other code
// works
$properties = $client->__soapCall($action, array(
array(
'Page' => $x
)
));
// try to get access to values by var $action
$obj = $properties->$action->item;
这给我一个错误。 我的意图是通过执行不同的操作将脚本仅用于多次操作一次。
...但是我不知道如何解决这个问题,也没有找到任何有用的帖子或文章。
var_dump($properties)
:
object(stdClass)#84 (4) {
["PropertyGroups"]=>
object(stdClass)#85 (1) {
["item"]=>
array(73) {
[0]=>
object(stdClass)#86 (7) {
["PropertyGroupID"]=>int(1)
}
}
}
}
脚本和这一行
$obj = $properties->$action->item;
绝对可以。 但是,正如您在评论中指出的那样,
如果action = GetPropertyGroups,未定义属性:
stdClass::$GetPropertyGroups
您在$properties
具有PropertyGroups
$properties
, 而不在GetPropertyGroups
。
您应该检查要查找的属性是否存在:
property_exists($properties, $action)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.