Edit report at https://bugs.php.net/bug.php?id=65296&edit=1
ID: 65296 Updated by: a...@php.net Reported by: llmll at gmx dot de Summary: Support named parameters in constructors to inline initialize objects with new() -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Scripting Engine problem Operating System: any PHP Version: Irrelevant Block user comment: N Private report: N New Comment: I really hope we won't turn php into javascript :) Please take a look, the sugar you talk about is possible with the following class Props { function __construct(array $argv) { foreach($argv as $arg => $val) { $this->$arg = $val; } } } $u = new Props(['hello' => 'world', 'good' => 'bye', 'obj' => new Props(['my' => 'god'])]); Or even use func_get_args() to make $argv optional. However this is a VERY bad style. Every object property should be defined with the class. Previous Comments: ------------------------------------------------------------------------ [2013-07-19 21:11:24] llmll at gmx dot de Just forgot to mention one more readability advantage: It would support "nested" initializations, like it is now possible with arrays full of strings $object = new Object({ Property: $aValue, Property2: $aValue2, Subobject: new Object({ ... }) }); ------------------------------------------------------------------------ [2013-07-19 21:05:31] llmll at gmx dot de Of course its working, this is not about a bug but about sleekness and PHP-to-programmer support ;-) These assignment-lists are really lengthy and contain too many repetitions. Its more like requesting the ternary operator instead of the "old" if ... then ... else ... ------------------------------------------------------------------------ [2013-07-19 20:56:31] mail+php at requinix dot net So object literals. Getting to use names instead of strings. Meanwhile $object = new Object(); $object->StringMember = "content"; // etc doesn't work? ------------------------------------------------------------------------ [2013-07-19 20:52:32] llmll at gmx dot de I view as a strength of PHP, not having to declare all fields in a class. For example think of a class representing a HTML DOM element, like a link. It allow many fields, but most often you will only use some of them, like "href" or "class". In my opinion the best syntax to write is something like: $link = new Link(href: "target.html", class:"btn btn-info"); At the moment I use arrays: $link = (new Link)->assign("href" => "target.html", "class" => "btn btn-info"); But the fields in the strings are messy and un-recognizeable. Phpdoc is far too cluttering. Declaring every possible option is pointless for some classes, especially if they are highly dynamic, like derived from a database table. ------------------------------------------------------------------------ [2013-07-19 17:42:05] mail+php at requinix dot net Keeping in mind that your IDE could not possibly give you support for arbitrary properties on objects, they probably do support the @property phpdoc. Add that to __get/__set and you have /** * @property string $StringMember * @property int $Counter * @property mixed[] $ArrayMember */ class Foo { private $data; public function __construct(array $data) { $this->data = $data; } public function __get($name) { return $this->data[$name]; } public function __set($name, $value) { $this->data[$name] = $value; } } $foo = new Foo([ "StringMember" => "content", "Counter" => 42, "ArrayMember" => ["old" => "way"] ]); $foo-> // should autocomplete/suggest StringMember, Counter, and ArrayMember If you're talking about the {} syntax you used, https://wiki.php.net/rfc/objectarrayliterals ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=65296 -- Edit this bug report at https://bugs.php.net/bug.php?id=65296&edit=1