En caso de que alguien más quiera encontrar una propiedad profunda o Para una profundidad desconocida, se me ocurrió lo siguiente sin necesidad de recorrer todas las propiedades conocidas de todos los niños.
Por ejemplo, para encontrar $ Foo-> Bar-> baz, o $ Foo-> baz, o $ Foo-> Bar-> Baz-> dave, donde $ path es una cadena como 'foo/bar/baz '.
public function callModule($pathString, $delimiter = '/'){
//split the string into an array
$pathArray = explode($delimiter, $pathString);
//get the first and last of the array
$module = array_shift($pathArray);
$property = array_pop($pathArray);
//if the array is now empty, we can access simply without a loop
if(count($pathArray) == 0){
return $this->{$module}->{$property};
}
//we need to go deeper
//$tmp = $this->Foo
$tmp = $this->{$module};
foreach($pathArray as $deeper){
//re-assign $tmp to be the next level of the object
// $tmp = $Foo->Bar --- then $tmp = $Bar->baz
$tmp = $tmp->{$deeper};
}
//now we are at the level we need to be and can access the property
return $tmp->{$property};
}
y luego llamar con algo como:
$propertyString = getXMLAttribute('string'); // '@Foo/Bar/baz'
$propertyString = substr($propertyString, 1);
$moduleCaller = new ModuleCaller();
echo $moduleCaller->callModule($propertyString);
gracias un montón! –
¡Cosas geniales +1, y gracias! –
¿Alguna ventaja de usar la segunda opción sobre la primera? – Clox