Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47660 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32292 invoked from network); 27 Mar 2010 17:09:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Mar 2010 17:09:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 83.243.58.134 as permitted sender) X-PHP-List-Original-Sender: johannes@php.net X-Host-Fingerprint: 83.243.58.134 mailout2.netbeat.de Linux 2.6 Received: from [83.243.58.134] ([83.243.58.134:33507] helo=mailout2.netbeat.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/12-22155-FCB3EAB4 for ; Sat, 27 Mar 2010 12:09:37 -0500 Received: (qmail 6898 invoked by uid 89); 27 Mar 2010 17:05:18 -0000 Received: from unknown (HELO ?192.168.1.28?) (postmaster%schlueters.de@93.104.41.168) by mailout2.netbeat.de with ESMTPA; 27 Mar 2010 17:05:18 -0000 X-Originator: 9e51b244e0a38413ab6a9876e36ba9df To: Toorion Cc: internals@lists.php.net In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Organization: php.net Date: Sat, 27 Mar 2010 18:09:28 +0100 Message-ID: <1269709768.7944.17.camel@guybrush> Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Proposal: shorthand object property setting syntax. From: johannes@php.net (Johannes =?ISO-8859-1?Q?Schl=FCter?=) 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