Thanks for the feedback!

пт, 29 мая 2026 г., 19:06 Larry Garfield <[email protected]>:
> My first thought is that the current code, which assigns potentially null 
> values, works fine if you throw an array_filter() at it at the end.
>
> $array = [
>     'field1' => $param1,
>     'field2' => $param2,
>     'field3' => $param3_which_is_null,
> ];
>
> $a = array_filter($array);
> // $a now omits the null fields

array_filter will only cover the cases when you need to filter all
fields by the same criteria (null's in this case).
But it does not fit the case when you still have some mandatory fields
even if they are null, but some are optional.
At least in the simplest form, but otherwise in more complex forms
we'll still come up with additional code.

> My second thought is that this is yet another reason why using associative 
> arrays as if they were a data structure is wrong and should be avoided; just 
> use a class and everything will be fine.  If you need to serialize it later, 
> there's many serializers on the market if JsonSerialize isn't sufficient.

Wrong or not, using arrays as an intermediate for serializing is still
very common.
Also, it is a common case when developers have their in-house
serialization mechanisms.

But whatever serialization you use, the serialization library by itself will
still need to perform corresponding "if"'s or other logic to
conditionally include/exclude the fields.
So, such feature can prove useful for serialization libraries themselves.

Another thing is that we don't only work with production-ready code
which presumably already
has or should have a proper "infrastructure" for serialization. We can
still work with some draft
or MVP projects where we need to do things fast and simple first. And
such feature can prove handy.

Reply via email to