On Sat, 2010-03-27 at 19:02 +0300, Toorion wrote:
> Very often (for example when work with dom or UI object) setting plenty 
> of object properties is require. At this time we has not a lot options.
> Standard way looks very dirty and be reason of more copy-paste-work.
> 
> Reproduce code:
> ---------------
> $myLongNameObject = new MyLongNameObject();
> $myLongNameObject->property1 = '11111';
> $myLongNameObject->property2 = '22222';
> $myLongNameObject->property3 = '33333';
> $myLongNameObject->property4 = '44444';
> $myLongNameObject->property5 = '55555';

You can always use a (temporary) alias to shorten things

$myLongNameObject = new MyLongNameObject();
$t = $myLongNameObject;
$t->property1 = '11111';
$t->property2 = '22222';
$t->property3 = '33333';
$t->property4 = '44444';
$t->property5 = '55555';
unset($t);

> Or worse case:
> $myLongNameObject = new MyLongNameObject(); // Proxy pattern
> $myLongNameObject->insideObject = new InsideObject();
> $myLongNameObject->insideObject->propertyOne   = '1111';
> $myLongNameObject->insideObject->propertyTwo   = '2222';
> $myLongNameObject->insideObject->propertyThree = '3334';
> $myLongNameObject->insideObject->propertyFour  = '4444';

$t = $myLongNameObject->insideObject;
$t->propertyOne = '11111';
$t->propertyTwo = '22222';


> So, apparently, that is not good. We can use special constructor code 
> and array like parameter of constructor like this:
> $MyLongNameObject = new MyLongNameObject( array(
>      'property1' = '1111',
>      'property2' = '2222',
>      .....
> ));

Better API design would be my first choice, details depend on the
specific case.

> But it's look like crutch and can't extended with IDE. Match better if 
> we can set multiple properties in one time something like this:
> 
> $MyLongNameObject = new MyLongNameObject() {
>      $property1 = '1111';
>      $property2 = '2222';
>      $property3 = '4444';
>      $property4 = '5555';
> }

That won't work easily as there's a conflict between other local
variables. So this might only work with very specific expressions,which
is then mostly useless. Syntax-wise it /could/  work like this:

with ($MyLongNameObject) {
    ->property1 = '1111';
    ->property2 = '2222';
}

but there's no big benefit over an alias but way more confusion with a
new syntax, a new keyword, ...

No need to add something special here.

johannes




-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to