Bug #79384 [Opn->Nab]: property_exists on ArrayObject
Edit report at https://bugs.php.net/bug.php?id=79384&edit=1
ID: 79384
Updated by: requinix@php.net
Reported by: michal dot bundyra at gmail dot com
-Summary: property_exists on Traversable
+Summary: property_exists on ArrayObject
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: ANY
PHP Version: 7.4.3
Block user comment: N
Private report: N
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This has nothing to do with Traversable.
https://www.php.net/manual/en/migration74.incompatible.php#migration74.incompatible.spl
Previous Comments:
------------------------------------------------------------------------
[2020-03-15 12:57:06] michal dot bundyra at gmail dot com
Description:
------------
As of PHP 7.4 using array_key_exists on objects is deprecated. The suggestion is to use
isset or property_exists instead.
The behaviour or both is different - one can detect null and the other can't.
Before in code we had:
```
$valueExists = array_key_exists($key, $data);
```
and it was working for any Traversable|array $data.
We are trying update our code and I thought it would be:
```
$valueExists = is_array($data) && array_key_exists($key, $data)
|| $data instanceof Traversable && property_exists($data, $key);
```
but unfortunately it is not working as expected.
Test script:
---------------
Please consider the following example:
```
$ao = new ArrayObject(['a' => null]);
var_dump($ao instanceof Traversable); // true
var_dump(array_key_exists('a', $ao)); // true but reports deprecated notice on PHP 7.4+
var_dump(property_exists($ao, 'a')); // false -> I would expect true here, as $ao is
Traversable
```
Expected result:
----------------
property_exists works on Traversable as array_key_exists worked before.
Actual result:
--------------
property_exists does not work on every Traversable.
I've noted that it is possible to set ArrayObject::ARRAY_AS_PROPS flat on
ArrayObject and this might be the solution. But honestly I am not sure if it is by design or should
be fixed.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79384&edit=1
Thread (2 messages)