Replies: 1 comment 1 reply
-
I don't think there's an easier way other than using if's or using nulls since we don't have a thing like operator overloading in PHP. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have LaravelData classes that look something like this:
With an instance of
A
being a field on an Eloquent model, I want to be able to access something like$myObj->a->c->str
in the simplest way possible, but also easily show a default value if the value is not set.If I used nullable (e.g.
public ?C $c
instead ofC|Optional
), I would be able to use a nullsafe operator like this:$myObj->a?->c?->str ?: 'default
. However, using nullable instead of Optional means that unset properties are written as explicit nulls in the database, which is not what I want.I don't want to be stuck writing lots of
if
s (or nasty nested ternaries) like this:Beta Was this translation helpful? Give feedback.
All reactions