php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80162 array_key_exists() should support ArrayAccess
Submitted: 2020-09-29 23:11 UTC Modified: -
From: divinity76 at gmail dot com Assigned:
Status: Open Package: Unknown/Other Function
PHP Version: Next Minor Version OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-09-29 23:11 UTC] divinity76 at gmail dot com
Description:
------------
i suggest changing `array_key_exists ( mixed $key , array $array )` to `array_key_exists ( mixed $key , array|ArrayAccess $array )` , it's awkward that array_key_exists can't check ArrayAccess objects

(and it has caused invalid bugreports in the past, ref https://bugs.php.net/bug.php?id=46354 and https://bugs.php.net/bug.php?id=34849 )

Test script:
---------------
<?php
class C implements ArrayAccess {
    public $data = array(
        "foo"=>"bar"
    );
    public function offsetExists ( $offset ): bool{
        return array_key_exists($offset,$this->data);
    }
    public function offsetGet ( $offset ){
        return $this->data[$offset];
    }
    public function offsetSet ( $offset , $value ): void {
        $this->data[$offset]=$value;
    }
    public function offsetUnset ( $offset ): void {
        unset($this->data[$offset]);
    }

}

$o=new C();

var_dump(array_key_exists('foo',$o));

Expected result:
----------------
bool(true)

Actual result:
--------------
Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, C given in /in/ZrcrF:24
Stack trace:
#0 {main}
  thrown in /in/ZrcrF on line 24

Process exited with code 255.


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 19:01:23 2020 UTC