Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:86916 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 71718 invoked from network); 26 Jun 2015 20:30:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jun 2015 20:30:11 -0000 Received: from [127.0.0.1] ([127.0.0.1:19314]) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ECSTREAM id 24/01-59313-356BD855 for ; Fri, 26 Jun 2015 16:30:11 -0400 Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.177 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.223.177 mail-ie0-f177.google.com Received: from [209.85.223.177] ([209.85.223.177:34570] helo=mail-ie0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B9/F0-59313-482BD855 for ; Fri, 26 Jun 2015 16:13:56 -0400 Received: by iebmu5 with SMTP id mu5so82517092ieb.1 for ; Fri, 26 Jun 2015 13:13:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=FgZVZxnJ0HkA5jn6qo6YIB9GSySxqWKqD3anlG/SlFQ=; b=oDPSEM/oZBYPpcn7dkzlo3EGGdw+WIC9blmLm7aeuxjNYqj0QYq8lU2e4iNPXtJ5wt USb8rZPBsbQnk2FAuc10lq2SAZ+uSOsnUo1DK0W9lii63A656QnGvbJfMRhAxQ6o+5Z9 Z90rc8UBt4uNf70dsyF0Aqx4VHzlWqtr9ZA2TT77uLobFRgjL3RMukrE/Xdb/JiY9o6P 9ODqbpqSW1lg+iaS4Q1iTJEa+RKf8vhM8rLA4tii/HQoXDNFaITrPtZSM2/g2e7YFoj6 gROuTxivMwj2J2sOnSD2h8PwT4/jnPgRXagxk4x36UL5/HJW9x9wkk0mr7lFpC7j5YY8 VySw== X-Received: by 10.107.25.15 with SMTP id 15mr4685014ioz.11.1435349422742; Fri, 26 Jun 2015 13:10:22 -0700 (PDT) MIME-Version: 1.0 Received: by 10.79.105.68 with HTTP; Fri, 26 Jun 2015 13:10:03 -0700 (PDT) Date: Fri, 26 Jun 2015 17:10:03 -0300 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary=001a113fd3b078a25f05197154ba Subject: RFC: Initialize objects by using an object initializer. From: david.proweb@gmail.com (David Rodrigues) --001a113fd3b078a25f05197154ba Content-Type: text/plain; charset=UTF-8 Hello, I tried to register in RFC Wiki but I get the error "That wasn't the answer we were expecting", but in all cases, I'm David Rodrigues, and I like to suggest a method to initialize objects by using an object initializar. Currently it is supported on C# -- read here .. Basically, it'll allow you create a new instance of some object and set your properties directly. *Class example:* class Example { public $a; protected $b; private $c; private $d; public function __construct($d) { $this->d = $d; } } *Currently:* *// Public scope:* $example = new Example($d); $example->a = $a; $example->b = $b; // error, protected $example->c = $c; // error, private return $example; *// Protected scope:* $example = new Example($d); $example->a = $a; $example->b = $b; $example->c = $c; // error, private return $example; *// Private scope:* $example = new Example($d); $example->a = $a; $example->b = $b; $example->c = $c; return $example; *Using object initializer:* *// Public scope:* return new Example($d) { $a => $a, $b => $b, // error, protected $c => $c, // error, private }; *// Protected scope:* return new Example($d) { $a => $a, $b => $b, $c => $c, // error, private }; *// Private scope:* return new Example($d) { $a => $a, $b => $b, $c => $c, }; *Variation:* *// Constructor without required arguments:* return new User { $name => "John", $surname => "Doe", }; *// Object initializer as array:* return new User [ $name => "John", $surname => "Doe", ]; *Others RFCs:* We have two RFCs about that, one was declined (Automatic Property Initialization ), other in draft (Constructor Argument Promotion ). This second one, I think that not is too clear what I'm trying to do, just by set visibility keywords before argument (like __construct(private $name)). And too, it'll affect all methods parsing, by checking if it is the __construct method, and if was setted visibility keywords. In my suggestion, that is similar to C#, it'll be done after the class initialization, probability less costly to parser. Probably it'll affect this: new_expr: T_NEW class_name_reference ctor_arguments object_initializer { $$ = zend_ast_create(ZEND_AST_NEW, $2, $3); } | T_NEW anonymous_class { $$ = $2; } ; object_initializer: /* empty */ | '{' object_initializer_list '}' -- David S. Rodrigues --001a113fd3b078a25f05197154ba--