php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77897 Inconsistency when ArrayAccess key casting occurs
Submitted: 2019-04-15 13:47 UTC Modified: 2019-04-15 13:51 UTC
From: bradyn at bradynpoulsen dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 7.2.17 OS: macOS 10.14.4
Private report: No CVE-ID: None
 [2019-04-15 13:47 UTC] bradyn at bradynpoulsen dot com
Description:
------------
When implementing ArrayAccess, I was expecting to be able to preserve the original type of keys before they are used inside of an `array()` instance.

In PHP 7.2.17, string keys (and only keys of this type) are being cast down to integers, if they meet the qualifications explained in the array documentation (https://www.php.net/manual/en/language.types.array.php).

This seems like inconsistent behavior that should not be evaluated until a string is used on an instance of an `array()`.



Test script:
---------------
<?php

class A implements ArrayAccess
{
    public $accessedKeys = [];
    
    public function offsetGet($key)
    {
        array_push($this->accessedKeys, $key);
    }
    
    public function offsetExists($key)
    {
        throw new BadMethodCallException();
    }
    
    public function offsetSet($key, $value)
    {
        throw new BadMethodCallException();
    }
    
    public function offsetUnset($key)
    {
        throw new BadMethodCallException();
    }
}

$test = new A();
$test[1];
$test["1"];
$test["01"];
$test[1.0];
$test[true];

var_dump($test->accessedKeys);

Expected result:
----------------
I expect array key casting not to happen until a key is accessed or stored on an `array()` instance.

I would expect the above output to be:

array(5) {
  [0]=>
  int(1)
  [1]=>
  string(1) "1"
  [2]=>
  string(2) "01"
  [3]=>
  float(1)
  [4]=>
  bool(true)
}

Actual result:
--------------
Instead, the string key (and only the string key) is converted when used via ArrayAccess methods.

The above output actually is:

array(5) {
  [0]=>
  int(1)
  [1]=>
  int(1)
  [2]=>
  string(2) "01"
  [3]=>
  float(1)
  [4]=>
  bool(true)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-15 13:51 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-04-15 13:51 UTC] nikic@php.net
This issue has been fixed in PHP 7.3, see https://3v4l.org/UY5vC.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Aug 20 23:01:34 2019 UTC