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
<https://msdn.microsoft.com/en-us/library/bb397680.aspx?f=255&MSPPError=-2147217396>
..

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 <https://wiki.php.net/rfc/automatic_property_initialization>),
other in draft (Constructor Argument Promotion
<https://wiki.php.net/rfc/constructor-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

Reply via email to