Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:47662 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 51070 invoked from network); 27 Mar 2010 17:57:22 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Mar 2010 17:57:22 -0000 X-Host-Fingerprint: 79.165.172.25 unknown Received: from [79.165.172.25] ([79.165.172.25:2573] helo=localhost.localdomain) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id AD/16-22155-1074EAB4 for ; Sat, 27 Mar 2010 12:57:21 -0500 Message-ID: To: internals@lists.php.net Date: Sat, 27 Mar 2010 20:57:14 +0300 User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.8) Gecko/20100301 Fedora/3.0.3-1.fc12 Thunderbird/3.0.3 MIME-Version: 1.0 References: <4BAE30F7.5090004@divbyzero.net> In-Reply-To: <4BAE30F7.5090004@divbyzero.net> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 79.165.172.25 Subject: Re: [PHP-DEV] Proposal: shorthand object property setting syntax. From: toorion@gmail.com (Toorion) On 03/27/2010 07:23 PM, Martin Jansen wrote: > On 27.03.10 17:02, Toorion wrote: >> $myLongNameObject = new MyLongNameObject(); >> $myLongNameObject->property1 = '11111'; >> $myLongNameObject->property2 = '22222'; >> $myLongNameObject->property3 = '33333'; >> $myLongNameObject->property4 = '44444'; >> $myLongNameObject->property5 = '55555'; > > [...] > >> $MyLongNameObject = new MyLongNameObject() { >> $property1 = '1111'; >> $property2 = '2222'; >> $property3 = '4444'; >> $property4 = '5555'; >> } > > What exactly do you gain with the new syntax? Readable code. Simplicity of developing, debugging. So, I write short example. Actually I work with long UI code (DOM, PHP-ExtJS implementation, more other new UI solution) and that code look like this: $grid = new ExtGridPanel(); $grid->store = new JsonStore(); $grid->store->autodestroy = true; $grid->store->url = 'plain.xml'; $grid->store->reader = new JesonReader(); $grid->store->reader->record = 'plant'; $grid->store->reader->fields = array(); $i = 0; $grid->store->reader->fields[$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'common'; $grid->store->reader->fields[$i]->type = 'string'; $grid->store->reader->fields[++$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'botanical'; $grid->store->reader->fields[$i]->type = 'string'; $grid->store->reader->fields[++$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'light'; $grid->store->reader->fields[++$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'price'; $grid->store->reader->fields[$i]->type = 'float'; $grid->store->reader->fields[++$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'availDate'; $grid->store->reader->fields[$i]->type = 'date'; $grid->store->reader->fields[$i]->mapping = 'availability'; $grid->store->reader->fields[$i]->dateFormat = 'm/d/Y'; $grid->store->reader->sortinfo = new stdClass(); $grid->store->reader->sortinfo->field = 'common'; $grid->store->reader->sortinfo->direction = 'ASC'; $grid->renderTo = 'editor-grid', $grid->width = 600; $grid->height = 300; $grid->autoExpandColumn = 'common'; $grid->title = 'Edit plants'; $grid->frame = true; $grid->tbar = new ExtTBar(); .... more code with setting of tbar properties.... $grid->items = array(); $i = 0; $grid->items[$i] = new GridItem(); $grid->items[$i]->id = 'common'; $grid->items[$i]->header = 'Common name'; $grid->items[$i]->dataIndex = 'common'; $grid->items[$i]->width = 220; $grid->items[$i]->editor = new GridColumnEditor(); ... more properties of GridColumnEditor .... ... more properties of next few items .... So, as you can see, this code (small part of PHP-ExtJS UI implementation) absolute unreadable. Yes, I can use variables like this: $r = new JsonReader(); $r->record = 'plant'; $r->fields = array(); $i = 0; $r->fields[$i] = new StoreField(); $r->fields[$i]->name = 'common'; $r->fields[$i]->type = 'string'; $r->fields[++$i] = new StoreField(); $r->fields[$i]->name = 'botanical'; $r->fields[$i]->type = 'string'; And: $grid->store->reader = $r; But it is not a good programming style - Who know what is it $r?, $a, $b, $c.... Usually I put $reader, $store, etc, But when in code few $readers, $stores - It become more difficult to identify variable. With proposal shorthand setting I can write more compressive: $fields[] = new StoreField() { $name = 'common'; $type = 'string' } Instead of $i = 0; $grid->store->reader->fields[$i] = new StoreField(); $grid->store->reader->fields[$i]->name = 'common'; $grid->store->reader->fields[$i]->type = 'string'; So, this sample I show - very short ;) In many cases for describe UI required more code, with DOM and CSS structures. Maybe it is not for PHP 5.3 and 6.0, but I sure in future some possibility become necessary. Actually I didn't understood, what for needed unnamed Object initialization new ObjectName( unnamed parameter, unnamed parameter) if usually in construct we did: function __construct( $param1, $param2, $param3 ) { $this->attribute1 = $param1; $this->attribute2 = $param2; $this->attribute3 = $param3; } Much better if we can join attributes directly from object initialization. $instance = new ObectName() { $attr1 = 'value', $attr2 = 'value' ); That we can set any value of object and don't needed to make decision which of attributes is important and which not.