I’m attempting to set up a variable-based object operator in PHP, but am only able to accomplish what I am looking for to a limited extent. For example, the following code allows for variable selection:
$var1 = 'available_from';
$keyValuePairs[$key] = $item->parent()->{$var1};
However, if I want to make the parent selector a variable as well, I no longer seem to be able to. Both of the following methods fail:
$var1 = 'parent()->available_from';
$keyValuePairs[$key] = $item->{$var1};
and
$var1 = 'parent()';
$var2 = 'available_from';
$keyValuePairs[$key] = $item->{$var1}->{$var2};
So the question is whether there is a way to do this.
You can basically do that, but you have to put the parens on the outside.
And there basically is no way of getting around that without using eval:
But, there is really no reason to use eval if you have access to the potential set of variables first.
You can do something like this to get around it: