Bug #77897 [Opn->Csd]: Inconsistency when ArrayAccess key casting occurs
| From: | nikic@php.net | Date: | Mon, 15 Apr 2019 13:51:06 +0000 |
| Subject: | Bug #77897 [Opn->Csd]: Inconsistency when ArrayAccess key casting occurs | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220466@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77897&edit=1
ID: 77897
Updated by: nikic@php.net
Reported by: bradyn at bradynpoulsen dot com
Summary: Inconsistency when ArrayAccess key casting occurs
-Status: Open
+Status: Closed
Type: Bug
Package: *General Issues
Operating System: macOS 10.14.4
PHP Version: 7.2.17
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This issue has been fixed in PHP 7.3, see https://3v4l.org/UY5vC.
Previous Comments:
------------------------------------------------------------------------
[2019-04-15 13:47:37] 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)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77897&edit=1