Re: RFC: Not Null Assertion Operator

From: Date: Tue, 11 Feb 2025 21:32:15 +0000
Subject: Re: RFC: Not Null Assertion Operator
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Tue, 11 Feb 2025, 17:07 Valentin Udaltsov, <[email protected]>
wrote:

>
> > Static analysis already complain about it
>
> That's exactly where ! is helpful. If at a certain point I am sure that
> a property must not be null and I want to make that explicit (to both
> developer and static analyzer), I can use
> assert($obj->prop !== null). However, that requires an extra line of
> code. $obj->prop!->x would be much prettier.
>
> --
> Valentin
>

Hi Valentin,

I still don't see the added value. PHP isn't Java—there are no hidden null
pointer exceptions. Accessing a property on null triggers a runtime
warning, and static analysis already flags it. It’s better if PHP throws an
error in such cases, as it does when calling methods on null.

Since types don’t include null by default, and assigning null to a typed
property already throws an error, I don’t think an additional operator is
necessary.

Best,
Faizan

>


Thread (9 messages)

« previous php.internals (#126376) next »