> Le 3 nov. 2022 à 02:51, Josh Bruce <j...@joshbruce.dev> a écrit :
> 
> Similar to:
> 
> function x(?SomeType $arg): ?SomeOtherType
> 
> Instead of:
> 
> function x(SomeType|null $arg): SomeType|null
> 
> Both are the same thing under the hood.

The big difference between `?SomeType` shortcut and falsifiable object feature, 
is that using `?SomeType` does not change the semantics of `SomeType|null`, 
while declaring an object falsifiable changes the semantics of `if ($obj)`.

> 
> In that same spirit, totally opt-in - not required to use or you can get 
> there some other way - similar to Marco’s comment around __toString. 



It is opt-in for the implementer of the library, not for the user of it, as 
they are not necessarily the same person.

Today, in a common situation where I know that a variable contains either an 
object of some type or null, I routinely write `if ($a)` to mean `if ($a !== 
null)`. Now, with magic `__toBool()` possibly implemented by  the library’s 
author, I don’t know what `if ($a)` means, unless I have carefully read the 
docs in order to know whether they have opt into that misfeature, and, if so, 
what exactly “falsy” means for them (invalid?, empty?, zero?). Ironically, that 
will incite me to not use anymore the implicit casting to bool for objects, and 
to write systematically `if ($a !== null)`, so that both myself and the reader 
of my code won’t have to look at the docs in order to understand such simple 
code.

—Claude

Reply via email to