I have a function where I want to work with an object.
(我有一个要在其中使用对象的函数。)
I would like to specify a child of that object with a string. (我想用字符串指定该对象的子代。)
I know the child is possible with $x
, but is it possible to go deeper with something like $y
, too? (我知道孩子可以用$x
,但是也可以像$y
一样做得更深吗?)
<?php
$obj = new stdClass;
$obj->token = new stdClass;
$obj->token->id = 123;
$x = 'created';
$obj->token->{$x} = 456;
$y = 'token->updated';
$obj->{$y} = 789;
print_r($obj);
?>
Obviously, that doesn't work, but I would like to get
(显然,这行不通,但我想得到)
stdClass Object
(
[token] => stdClass Object
(
[id] => 123
[created] => 456
[updated] => 789
)
)
Thank you!
(谢谢!)
ask by Josef Habr translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…