Open julien-boudry opened 2 years ago
I wonder if that is deliberate or if s/IS_NULL/IS_UNDEF
would be more appropriate.
This is deliberate. Despite the confusing name, the API contract of offsetExists is that it must return false for null values. It is the analogue of __isset()
and must match isset()
semantics.
This is deliberate. Despite the confusing name, the API contract of offsetExists is that it must return false for null values. It is the analogue of
__isset()
and must matchisset()
semantics.
Will other Spl implementations of ArrayAccess, like SplObjectStorage, will change in the future according to this?
var_dump( isset($os[$obj1]) );
return true.Despite the confusing name, the API contract of offsetExists is that it must return false for null values. It is the analogue of
__isset()
and must matchisset()
semantics.
Ah, right.
Will other Spl implementations of ArrayAccess, like SplObjectStorage, will change in the future according to this?
SplObjectStorage::offsetExists()
is an alias of ::contains()
, what is apparently a deliberate design decision:
https://github.com/php/php-src/blob/7ce5e0a8f166142f64edfc2cd1f92c45b9c0b946/ext/spl/spl_observer.c#L459
I think we should clarify the respective note in the manual.
Are there other classes, where ::offsetExists()
exhibits this behavior?
I think we should clarify the respective note in the manual.
Also on ArrayAccess manual, is unclear ( https://www.php.net/manual/en/arrayaccess.offsetexists.php ) even if it's talking about the empty() function
Just encountered this myself (gettype($splos[$i])
is NULL (actually, never assigned to) but isset($splos[$i])
is true), and found nothing to suggest the behaviour was correct.
The only part of the manual I found that was relevant was the statement on the isset
manual page that it returns false for a variable that has been assigned null.
Is it correct? If it's a deliberate decision, what was the rationale? Or is it just a emergent consequence of offsetExists
/contains
being aliased together?
Just encountered this myself (
gettype($splos[$i])
is NULL (actually, never assigned to) butisset($splot[$i])
is true), and found nothing to suggest the behaviour was correct.
I can't reproduce this. https://3v4l.org/NKKhE
Additionally, "never assigned to" suggests you didn't create $i
in the $splot
WeakMap, in which case gettype-ing it will throw an exception because it doesn't exist, so I'm not sure what you mean by that.
Unless, of course, the splos
/splot
typo in your comment is also present in your code?
Try using SplObjectStorage
instead of WeakMap
per the comment from @cmb69 above. The issue seems more generally around the behaviour of offsetExists
. I'll fix that typo.
In perhaps related behaviour, isset($storage[$unsetvar])
will spit warnings when $unsetvar
is not initialised; that case is not dealt with before passing it off to the offsetExists
method. Would moving unset/null tests up so they're done ahead of calling SPL methods be a way of addressing these in a general sense?
I had a quick look at that. Unfortunately, there attach
method deliberately uses NULL
as an empty value. So, changing the semantics to match isset
means we'll also need to change this empty value. There's also this comment:
So this seems intentional, although dubious.
In perhaps related behaviour,
isset($storage[$unsetvar])
will spit warnings when$unsetvar
is not initialised
Not sure why I thought this was a problem when I wrote that: it's always been normal behaviour. The test is whether the indexed element exists or not - which assumes the index exists.
Description
The following code:
Resulted in this output:
But I expected this output instead:
PHP Version
PHP 8.1.5
Operating System
*