[TYPO3-50-general] Helping Functions

Nino Martincevic don at zampano.com
Mon Feb 2 13:41:07 CET 2009


This concept is also well-known as "virtual properties".
The guys of EZ.no are using such things in their component framework. 
See example class below. But even without the need for get* and set* 
methods.

Which is, by the way, in most cases an "getter/setter anti-pattern", 
sadly seen in far to many places, if used this way:

private $bla;
function getBla() {
  return $this->bla;
}

Why you have a getter if you set it private?
Or why you set it private when you provide a getter?

Use only getters when you do more then just returning the property, e.g. 
calculate a value, but keep it side-effect free of course.

The problem with not seeing the class vars in class hints does not 
really exist. Because they should be encapsulated anyway with meaningful 
behavior, especially the setters. Unless your class(es) are pure data 
shapes (which they shouldn't be anyway).
And most classes should handle commands not just queries anyway (CQS & 
Tell, Don't Ask principle).
An alternative might also be the "Private Class Data" pattern.


Here's an EZ-like virtual property class (a little "blown up"):

class VirtualPropertyObject implements Serializable
{
     protected $properties = array();

     public function __construct( Array $properties )
     {
         $this->properties = $properties;
     }

     public static function create($properties)
     {
     	$c = new VirtualPropertyObject($properties);
     	return $c;
     }

     public function __set( $propertyName, $propertyValue )
     {
         $this->properties[$propertyName] = $propertyValue;
     }

     public function __get( $propertyName )
     {
     	if ( isset( $this->$propertyName ) )
         {
             return $this->$properties[$propertyName];
         }
         throw new RuntimeException( "$propertyName does not exist." );
     }

     public function __isset( $propertyName )
     {
         return ( array_key_exists( $propertyName, $this->properties ) );
     }

     public function serialize( )
     {
         return serialize($this->properties);
     }

     public function unserialize( $what )
     {
         $this->properties = unserialize($this->properties);
     }

}

Cheers,
Nino


Xavier Perseguers schrieb:
> Hi Sebastian,
> 
>>> Is there currently a way so I don't need to write all this set/get
>>> functions?
>> Currently (IMHO) we require you to write all getters / setters by hand. 
>> In the middle-long term, there will be scaffolding for that - but you 
>> should do that because the domain layer should not contain any "magic". 
>> When Eclipse supports namespace syntax, it'll be much nicer to work with 
>> if you have explicit getters/setters.
>> If you, however, require a way to access getters/setters 
>> programmatically, we've created helpers in Reflection/ObjectAccess.php. 
>> This is a kind of Java Beans implementation for PHP.
> 
> I'm using the method shown on [1] for my current 4.x development. It 
> works very well and the interesting part is that it is similar than 
> public property:
> 
> $myobject->someProperty = "hello world!";
> echo $myobject->someProperty;
> 
> instead of
> 
> $myobject->setSomeProperty("hello world!");
> echo $myobject->getSomeProperty();
> 
> which I find better and similar to C#.
> 
> The overhead of the method shown in term of definition of private and 
> public fields that are then overriden at instantiation time is a bit 
> high though. But I would love to have such feature (enhanced of course) 
> in FLOW3.
> 
> [1] http://latrine.dgx.cz/property-setters-and-getters-final-solution
> 



More information about the TYPO3-project-5_0-general mailing list