php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69799 Integer as object property. Malformed array.
Submitted: 2015-06-11 13:37 UTC Modified: 2015-06-11 18:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: miloslav dot hula at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant 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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: miloslav dot hula at gmail dot com
New email:
PHP Version: OS:

 

 [2015-06-11 13:37 UTC] 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)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-06-11 18:22 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-06-11 18:22 UTC] cmb@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 05:01:30 2024 UTC