Req #65296 [Com]: Support named parameters in constructors to inline initialize objects with new()

From: Date: Fri, 19 Jul 2013 20:56:32 +0000
Subject: Req #65296 [Com]: Support named parameters in constructors to inline initialize objects with new()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-180354@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=65296&edit=1

 ID:                 65296
 Comment by:         mail+php at requinix dot net
 Reported by:        llmll at gmx dot de
 Summary:            Support named parameters in constructors to inline
                     initialize objects with new()
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   any
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

So object literals. Getting to use names instead of strings.

Meanwhile

$object = new Object();
$object->StringMember = "content";
// etc

doesn't work?


Previous Comments:
------------------------------------------------------------------------
[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

------------------------------------------------------------------------
[2013-07-19 10:25:40] llmll at gmx dot de

typo in summary corrected

------------------------------------------------------------------------
[2013-07-19 10:20:30] llmll at gmx dot de

Description:
------------
As PHP does not support *real* overloading by different parameter sets, arrays are being used for
dynamic parameters as well as for object initialization in constructors.

However arrays use strings as keys, which kills syntax highlighting and refactoring capabilities in
IDEs.

PHP engine should provide a way to initialize an object inline by dynamically providing parameters
to new().


Test script:
---------------
$object = new Object({
    StringMember: "content",
    Counter: 42,
    ArrayMember: ["old" => "way"]
});

Expected result:
----------------
$object->StringMember = "content";
$object->Counter = 42;
$object->ArrayMember = array("old" => "way");
$object->MoreProperties = "stay untouched";

Actual result:
--------------
not possible.


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65296&edit=1


Thread (12 messages)

« previous php.bugs (#180354) next »