Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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');

?>

Alternatives

  • 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().

In more recent PHP versions, this error message is now :ref:argument-#%d-($%s)-must-be-of-type-%s,-%s-given.