[TYPO3-mvc] FLUID :: object accessor syntax :: nested objects possible?

Bastian Waidelich bastian at typo3.org
Mon Jul 2 10:06:42 CEST 2012


Benjamin Kott wrote:

Hi,


>
>     class Tx_MyExt_ViewHelpers_DisplayViewHelper extends
> Tx_Fluid_Core_ViewHelper_AbstractViewHelper {
>
>          /**
>       * @param $obj  object Object
>       * @param $prop    string Property
>       */
>          public function render($obj,$prop) {
>              if(is_object($obj)) {
>                  return $obj->$prop;
>              } elseif(is_array($obj)) {
>                  if(array_key_exists($prop, $obj)) {
>                          return $obj[$prop];
>                  }
>              }
>              return NULL;
>      }
>
>      }
>
> ?>

You can also use the utility methods of Tx_Extbase_Utility_ObjectAccess. 
Those already handle arrays vs objects, find the correct getters and 
won't pose fatal errors if a property does not exist.

But apart from that:
@Roland do you have a more concrete example? Mostly it makes more sense 
to move such logic to your domain model. See also [1]


[1] 
http://forge.typo3.org/projects/typo3v4-mvc/wiki/FAQ#How-can-I-use-dynamic-array-indexes


-- 
Bastian Waidelich
TYPO3 Core Team Member

TYPO3 .... inspiring people to share!
Get involved: typo3.org




More information about the TYPO3-project-typo3v4mvc mailing list