Re: [RFC] Object Initializer
| From: | Paul M. Jones | Date: | Sun, 15 Sep 2019 13:48:45 +0000 |
| Subject: | Re: [RFC] Object Initializer | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-107108@lists.php.net to get a copy of this message | ||
> On Sep 12, 2019, at 09:00, 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
This reminds me of how Hack/HHVM would initialize object properties from constructor parameters; I
remember finding it very appealing.
IIRC, you would provide the property signature as a constructor parameter to trigger the
initialization:
```php
class Foo
{
protected int $bar;
public function __construct(protected int $bar)
{
}
public function getBar() : int
{
return $this->bar;
}
}
$foo = new Foo(1);
echo $foo->getBar(); // 1
```
Perhaps something like that is worth adopting here.
--
Paul M. Jones
pmjones@pmjones.io
http://paul-m-jones.com
Modernizing Legacy Applications in PHP
https://leanpub.com/mlaphp
Solving the N+1 Problem in PHP
https://leanpub.com/sn1php