Re: [RFC] Object Initializer

From: Date: Fri, 13 Sep 2019 09:39:25 +0000
Subject: Re: [RFC] Object Initializer
References: 1 2 3 4 5 6 7 8 9  Groups: php.internals 
Request: Send a blank email to internals+get-107070@lists.php.net to get a copy of this message
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

« previous php.internals (#107070) next »