Re: [RFC] Object Initializer
| From: | Olumide Samson | Date: | Thu, 12 Sep 2019 15:07:31 +0000 |
| Subject: | Re: [RFC] Object Initializer | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106971@lists.php.net to get a copy of this message | ||
The RFC is a beautiful feature suggestion, but something is telling me as
beautiful and straightforward the syntax is, what can the use case be?
I really love Constructor or Object initialization to be implemented in
PHP, but is there something I think you're missing in the RFC?
Thanks for the RFC.
On Thu, Sep 12, 2019, 4:02 PM Lynn <kjarli@gmail.com> wrote:
> On Thu, Sep 12, 2019 at 4:00 PM Michał Brzuchalski <
> michal.brzuchalski@gmail.com> wrote:
>
> > Hi internals,
> >
> > I'd like to open discussion about RFC: Object Initializer.
> >
> > This proposal reduces boilerplate of object instantiation and properties
> > initialization in case of classes without required constructor arguments
> as
> > a single expression with initializer block.
> >
> > https://wiki.php.net/rfc/object-initializer
> >
> > I appreciate any feedback you all can provide.
> >
> > Thanks,
> > --
> > Michał Brzuchalski
> > brzuchal@php.net
> >
> >
> Heya,
>
> What's the added benefit of this compared to implementing a constructor?
>
> The part I like is that this can be used to replace stdClass/structured
> arrays. Perhaps something like this would nice to have in PHP:
>
> ```
> $people = [];
>
> foreach ($peopleFromDatabase as [$id, $username, $name]) {
> $people[] = {
> Uuid id => $id,
> string username => $username,
> string name => $name,
> };
> // and possible automatic assignment:
> $people[] = {Uuid $id, string $username, string $name};
> }
> ```
>
> Regards,
> Lynn van der Berg
>