Re: [PHP-DEV] [RFC] Object Initializer

This is only part of a thread. view whole thread
  107078
September 13, 2019 13:23 matthewmatthew@gmail.com (Matthew Brown)
Though this is truly a stylistic complaint, I think it will lead to
harder-to-analyse code.

Currently I have a static analysis tool that can verify (in 99% of cases)
whether all properties of a class have been initialised in the constructor.
This check is even more important in PHP 7.4, where use of a property
without instantiation is a fatal error.

Figuring out which properties have been instantiated in a constructor is
non-trivial, and it's only efficient to do it once per class.

If we adopt this into the language and people use both __construct and
object initializers for a given class, analysis would become much more
tricky.

In order to find the error in this:

abstract class A {
  public string $s;
  public int $t;
}

class B extends A {
  public bool $u;

  public function __construct() {
    $this->s = "hello";
  }
}

$b = new C {
  u = true
};

The analyzer needs to understand that the initialisation of B left a
property uninitialised - it warns about it in B's constructor (
https://psalm.dev/r/0e8e40fefc) but I worry that people will start to
dismiss those warnings as false-positives if this pattern becomes popular.

Best wishes,

Matt




On Thu, 12 Sep 2019 at 10: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 > > I appreciate any feedback you all can provide. > > Thanks, > -- > Michał Brzuchalski > brzuchal@php.net >
  107089
September 14, 2019 09:05 sebastian@php.net (Sebastian Bergmann)
Am 13.09.2019 um 15:23 schrieb Matthew Brown:
> Though this is truly a stylistic complaint, I think it will lead to > harder-to-analyse code.
Fully agreed, and not just harder-to-analyse for a static analysis tool but also for humans that read the code. -1 from me.
  107090
September 14, 2019 09:21 michal@brzuchalski.com (=?UTF-8?Q?Micha=C5=82_Brzuchalski?=)
Hi Sebastian,

sob., 14 wrz 2019 o 11:05 Sebastian Bergmann <sebastian@php.net> napisał(a):

> Am 13.09.2019 um 15:23 schrieb Matthew Brown: > > Though this is truly a stylistic complaint, I think it will lead to > > harder-to-analyse code. > > Fully agreed, and not just harder-to-analyse for a static analysis tool > but also for humans that read the code. -1 from me. > > Could you explain why additional noise caused by constantly repeating
instance variable name and arrow in front of property name and assignment part is easier to read? Just wondering why you see object initializer as harder to read which I've view as easier to read. Example - easy to read: function createCustomerDTO(): Customer { $customer = new Customer(); $customer->id = Uuid::uuid4; $customer->name = "John Doe"; $customer->address = "Customer Street 16"; $customer->city = "London"; $customer->country = "GB"; $customer->phoneNumber = PhoneNumber::fromString("+1555 010 020"); $customer->birthDate = new DateTimeImmutable("1983-01-01"); $customer->email = Email::fromString("john.doe@example.com"); return $customer; } Example - hard to read: Why? function createCustomerDTO(): Customer { return new Customer { id = Uuid::uuid4, name = "John Doe", address = "Customer Street 16", city = "London", country = "GB", phoneNumber = PhoneNumber::fromString("+1555 010 020"), birthDate = new DateTimeImmutable("1983-01-01"), email = Email::fromString("john.doe@example.com"), }; } Thanks in advance, Michał Brzuchalski