Using array_key_exists() on objects is deprecated.¶
Description¶
array_key_exists() used to accept both arrays and objects. In array cases, it looks for an index in the array keys, and in the object case, it looks for a property in the public properties.
array_key_exists() only works with array: it doesn’t work with ArrayAccess object, and yields the same error.
In PHP 7.4, this feature was deprecated, with the eponymous message. In PHP 8.0, it is now turned into a type error: the second argument must be an array.
Example¶
<?php
class X {
public string $property = '';
}
$object = new X();
array_key_exists($object, 'index');
?>
Solutions¶
Check if the variable is actually an object before using it with array_key_exists().
Check if the variable is actually an object before using it with array_key_exists().