Bug #77897 [NEW]: Inconsistency when ArrayAccess key casting occurs
| From: | bradyn at bradynpoulsen dot com | Date: | Mon, 15 Apr 2019 13:47:37 +0000 |
| Subject: | Bug #77897 [NEW]: Inconsistency when ArrayAccess key casting occurs | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-220465@lists.php.net to get a copy of this message | ||
From: bradyn at bradynpoulsen dot com
Operating system: macOS 10.14.4
PHP version: 7.2.17
Package: *General Issues
Bug Type: Bug
Bug description:Inconsistency when ArrayAccess key casting occurs
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 bug report at https://bugs.php.net/bug.php?id=77897&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77897&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77897&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77897&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77897&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77897&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77897&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77897&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77897&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77897&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77897&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77897&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77897&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77897&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77897&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77897&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77897&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77897&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77897&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77897&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77897&r=mysqlcfg