Proposal: shorthand object property setting syntax.

From: Date: Sat, 27 Mar 2010 16:02:40 +0000
Subject: Proposal: shorthand object property setting syntax.
Groups: php.internals 
Request: Send a blank email to internals+get-47655@lists.php.net to get a copy of this message
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';

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

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',
    .....
)); 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';
} And (for multiple setting properties for exists object): $MyLongNameObject = new MyLongNameObject(); $MyLongNameObject->{
    $propertyOne   = '1111';
    $propertyTwo   = '2222';
    $propertyThree = '3333';
    $propertyFour  = '4444';
} So, this way can help in many cases with initialization long OOP structures and economy lot of time for programming, debugging and refactoring code.

Thread (15 messages)

« previous php.internals (#47655) next »