Hi Luigi On Fri, May 17, 2024 at 11:40 PM Luigi Cardamone <cardamonelu...@gmail.com> wrote: > > Here is an example to describe my problem. Imagine a simple > DTO like this: > > class MyDTO{ > public ?int $propA; > public ?int $propB; > } > > Imagine that a Form processor or a generic mapper fill some of > these fields with a null value: > > $dto = new MyDTO(); > $dto->propA = null; > > Sometimes we use DTOs to handle PATCH requests and not all > the properties are mapped with a value. In a scenario like this, > "null" is often a valid value. > > At this point, I need a way to find if a property was initialized or > not but unfortunately "isset" is not a solution. When I write: > > echo isset($dto->propA) ? 'init' : 'not-init';
IMO, "uninitialized" should not be treated as a value. Code outside of the constructor should generally not have to deal with uninitialized properties. Instead, make sure the constructor leaves your object fully initialized, in this case likely by setting it to null. If you need some additional "undefined" state, I think that's better modeled in other ways, maybe by wrapping it in an object. ADTs [1] should help with that in the future. enum Age { case Unknown; case Unborn; case Years(int $years); } public Age $age; Or: enum Age { case Unborn; case Years(int $years); } public ?Age $age; Silly example, but you get the point. Ilija [1] https://wiki.php.net/rfc/tagged_unions