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: -
Votes:4
Avg. Score:4.2 ± 0.4
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: divinity76 at gmail dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Nov 22 02:01:28 2024 UTC