Bug #69799 [Opn->Nab]: Integer as object property. Malformed array.
| From: | cmb@php.net | Date: | Thu, 11 Jun 2015 18:22:13 +0000 |
| Subject: | Bug #69799 [Opn->Nab]: Integer as object property. Malformed array. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193335@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69799&edit=1
ID: 69799
Updated by: cmb@php.net
Reported by: miloslav dot hula at gmail dot com
Summary: Integer as object property. Malformed array.
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: Irrelevant
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
This behavior is documented:
<http://php.net/manual/en/language.types.array.php#language.types.array.casting>.
Usually you will want to avoid integer properties on objects.
Previous Comments:
------------------------------------------------------------------------
[2015-06-11 13:37:53] miloslav dot hula at gmail dot com
Description:
------------
When integer used as object's property name, conversion to array by (array) makes normally
integer array keys as string. In result, isset() and array_key_exists() doesn't work on
malformed array.
Test script:
---------------
<?php
# available on http://3v4l.org/ambbi
class ArrayHash extends \stdClass
{
public static function from(array $arr)
{
$obj = new static;
foreach ($arr as $key => $value) {
$obj->$key = $value;
}
return $obj;
}
}
$one = array('abcdef' => 'xxx', '102787' => 'yyy');
$two = (array) ArrayHash::from($one);
echo "# One\n";
var_dump($one);
var_dump(isset($one['abcdef']));
var_dump(array_key_exists('abcdef', $one));
var_dump(isset($one[102787]));
var_dump(array_key_exists(102787, $one));
var_dump(isset($one['102787']));
var_dump(array_key_exists('102787', $one));
echo "\n\n";
echo "# Two\n";
var_dump($two);
var_dump(isset($two['abcdef']));
var_dump(array_key_exists('abcdef', $two));
var_dump(isset($two[102787]));
var_dump(array_key_exists(102787, $two));
var_dump(isset($two['102787']));
var_dump(array_key_exists('102787', $two));
echo "\n\n";
Expected result:
----------------
# One
array(2) {
["abcdef"]=>
string(3) "xxx"
[102787]=>
string(3) "yyy"
}
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
# Two
array(2) {
["abcdef"]=>
string(3) "xxx"
["102787"]=>
string(3) "yyy"
}
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
Actual result:
--------------
# One
array(2) {
["abcdef"]=>
string(3) "xxx"
[102787]=>
string(3) "yyy"
}
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
# Two
array(2) {
["abcdef"]=>
string(3) "xxx"
["102787"]=>
string(3) "yyy"
}
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69799&edit=1