|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2020-03-15 13:01 UTC] requinix@php.net
-Summary: property_exists on Traversable
+Summary: property_exists on ArrayObject
-Status: Open
+Status: Not a bug
[2020-03-15 13:01 UTC] requinix@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 07:00:01 2025 UTC |
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.