Hi Internals,

I would like to discuss the possibility of assigning multiple public
properties at once during the class initialization.
Currently, with constructor property promotion we've already achieved the
ability to skip a lot of boilerplate code. With property accessors (aka
hooks) we could rid of getters and setters. But often classes need
constructors only to set properties. In such cases, constructors become
redundant.

Here are a few examples:
```
class Person
{
    public string $firstName;
    public int $age;
}

$person = new Person {
   firstName: "John",
   age: 42
};
```

The initialization becomes "struct-like" but it should not affect the
current state and in general it makes code work like this:
```
$person = new Person();
$person->firstName = "John";
$person->age = 42;
```
In case if constructor exists, multiple property assignment just rewrites
values, so it could even be something like following:
```
class Person
{
    public function __construct(
        public string $firstName,
        public int $age
    ) {
    }

   $person = new Person("Name", 43) {
      firstName: "John",
      age: 42
   };
}
```

However, the syntax is the question and it could be done similarly to:
https://wiki.php.net/rfc/compact-object-property-assignment

Also, such "functionality" could be reused for cloning and maybe for other
appropriate cases:
```
$person = new Person {
   firstName: "John",
   age: 42
};

$person2 = clone $person {
   firstName: "John Doe",
   age: 43
}
```

---
Taras Chornyi

Reply via email to