Bug #71208 [Asn]: Array/Object cast loses numeric property/element accessibility

From: Date: Thu, 24 Dec 2015 01:01:42 +0000
Subject: Bug #71208 [Asn]: Array/Object cast loses numeric property/element accessibility
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-198192@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71208&edit=1 ID: 71208 Updated by: ajf@php.net Reported by: ajf@php.net Summary: Array/Object cast loses numeric property/element accessibility Status: Assigned Type: Bug Package: Scripting Engine problem Operating System: OS X 10.11 El Capitan PHP Version: 7.0.1 Assigned To: ajf Block user comment: N Private report: N New Comment: Er, the test script has an off-by-one error. It should be: <?php $arr = [1, 2, 4]; $obj = (object)$arr; var_dump($obj); var_dump($obj->{0}, $obj->{1}, $obj->{2}); var_dump($obj->{'0'}, $obj->{'1'}, $obj->{'2'}); ?> Previous Comments: ------------------------------------------------------------------------ [2015-12-23 23:52:02] ajf@php.net This has been broken since at least PHP 5.4. ------------------------------------------------------------------------ [2015-12-23 23:50:41] ajf@php.net Description: ------------ If an array is casted to an object, its integer keys become inaccessible integer(!) key properties. They should be normal string key properties instead. Test script: --------------- <?php $arr = [1, 2, 4]; $obj = (object)$arr; var_dump($obj); var_dump($obj->{1}, $obj->{2}, $obj->{3}); var_dump($obj->{'1'}, $obj->{'2'}, $obj->{'3'}); ?> Expected result: ---------------- object(stdClass)#1 (3) { ["0"]=> int(1) ["1"]=> int(2) ["2"]=> int(4) } int(1) int(2) int(4) int(1) int(2) int(4) Actual result: -------------- object(stdClass)#1 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(4) } Notice: Undefined property: stdClass::$1 in - on line 7 Notice: Undefined property: stdClass::$2 in - on line 7 Notice: Undefined property: stdClass::$3 in - on line 7 NULL NULL NULL Notice: Undefined property: stdClass::$1 in - on line 8 Notice: Undefined property: stdClass::$2 in - on line 8 Notice: Undefined property: stdClass::$3 in - on line 8 NULL NULL NULL ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71208&edit=1

« previous php.bugs (#198192) next »