php - Getting reference to array element where array is accessible as $obj->$propName -
suppose have code (simplified example):
$propertyname = 'items'; $foo = new \stdclass; $foo->$propertyname = array(42);
at point 'd write expression evaluates reference value inside array.
is possible this? if so, how?
an answer "do job" not 'm looking this:
// not acceptable: 2 statements $temp = &$foo->$propertyname; $temp = &$temp[0];
but why write 2 statements? well, because not work:
// not work: php's fantastic grammar strikes again // equivalent $temp = &$foo->i $temp = &$foo->$propertyname[0];
of course &$foo->items[0]
unacceptable solution because fixes property name.
in case wondering strange requirements: 'm doing inside loop $foo
reference node inside graph. solution involving $temp
require unset($temp)
afterwards setting $temp
on next iteration not mess graph; unset
requirement can overlooked if not ultra careful around references, wondering if there way write code such there less possibilities introduce bug.
ambiguous expressions need parser:
$temp = &$foo->{$propertyname}[0];
btw, same regardless if you're looking variable alias (a.k.a. reference) or value. both need if use array-access notation.
Comments
Post a Comment