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.







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

Reply via email to