Though I'd prefer only allowing the syntax with parentheses like set($value)
It could be nice addition to creating DTOs objects in PHP with "virtual" properties:
readonly final class SomeDto { public function __construct( public string $name, public string $surname, public string $fullName { get => ucfirst($this->name) . ' ' . ucfirst($this->surname); } ) { } }
or even shorter version:
readonly final class SomeDto { public function __construct( public string $name, public string $surname, public string $fullName => ucfirst($this->name) . ' ' . ucfirst($this->surname), ) { } }
Nice addition, but I would change syntax:
class User { public string $name { set($value) { if (strlen($value) === 0) { throw new ValueError("Name must be non-empty"); } return $value; } } public function __construct(string $name) { $this->name = $name; } }
This is good to have.
Properties are useful for exchanging (reading and writing) single values. Properties are good for data binding, etc.
With this RFC we can implement:
Update: About the $field I am not sure. Having a separate backing field can have some advantages.