Re: [PHP-DEV] [RFC] Object Initializer

This is only part of a thread. view whole thread
  107070
September 13, 2019 09:39 michal.brzuchalski@gmail.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
pt., 13 wrz 2019 o 11:29 Mike Schinkel <mike@newclarity.net> napisał(a):

> IMO should stay specific for arrays only. > > > Why? Is there an objective reason? > > $obj->foo => 123;
Simply, that's not the way you initialize object property values. When you use "=>" in array context you're pushing a new value to array and it'll work without labelling it with key where the key could be number index or string index in what shape you want (whitespaces etc.).
> Is there some aspect of the syntax that makes it appropriate for arrays > but inappropriate for object initialization? > > > These are honest question. > > > My two cents: I would like to type less too, but I have always assumed > that "=>" was the PHP-ish syntax for initialization of a structure so it > seems very appropriate to use for object initialization too. > > > That said, > > > 1. I don't feel strongly about iti either way, > > 2. Unless using "=>" would make naked object initialization possible — > i.e. { foo => 10 } — given that it seems, as Arnold said, this would > conflict with other meanings: { foo = 10 } > > > { $foo = 123 }; // unexpected "}" cause of missing ";"
$bar = { $foo = 123 }; // unexpected "{" cause it's not allowed in this context Both examples are syntax error. You can use {} for separating blocks of code, but now if you wanna assign value. Everything considered syntax error can be used for feature shaping. Regards, Michał Brzuchalski
  107072
September 13, 2019 09:47 mike@newclarity.net (Mike Schinkel)
> On Sep 13, 2019, at 2:39 AM, Michał Brzuchalski brzuchalski@gmail.com> wrote: > $obj->foo => 123; > Simply, that's not the way you initialize object property values.
So by the same logic this should be PHP's way to initialize array elements, right? $arr['foo'] => 123; But it is not. Obviously we both know that this is the correct syntax: $arr['foo'] = 123; So that is why I think it would be more consistent in PHP for object initializers to use "=>" instead of "=." But, I myself will not belabor the point beyond this message. Either others will agree with you or they will suggest to change to "=>" too.
> { $foo = 123 }; // unexpected "}" cause of missing ";" > $bar = { $foo = 123 }; // unexpected "{" cause it's not allowed in this context > Both examples are syntax error. > You can use {} for separating blocks of code, but now if you wanna assign value. > Everything considered syntax error can be used for feature shaping.
Hmm. Ok, I will let Arnold reply to you on this if he feels that your reply did not address his concerns. -Mike