1

When I Do this:

<?php

$object = array(
    'key' => 'value',
    1 => 'value',
    '11' => 'value',
    '22' => 'value'
);

print_r($object); echo'<br>';

$object = (object)$object;

print_r($object); echo'<br>';echo'<br>';

//ok looks good so far but then

echo $object->key       . '<br>'; // good
echo $object->{1}       . '<br>'; // bad 
echo $object->{11}      . '<br>'; // bad
echo $object->{'22'}    . '<br>'; // bad

$object->key = 'changed value';
$object->{1} = 'changed value';
$object->{'11'} = 'changed value';
$object->{'22'} = 'changed value';

echo'<br>'; print_r($object); echo'<br>';echo'<br>';

echo $object->key       . '<br>'; // good
echo $object->{1}       . '<br>'; // good 
echo $object->{11}      . '<br>'; // good
echo $object->{'22'}    . '<br>'; // good

$object = (array)$object;

echo'<br>'; print_r($object);echo'<br>';echo'<br>';

echo $object['key']     . '<br>'; // good
echo $object[1]         . '<br>'; // wtf 
echo $object['11']      . '<br>'; // wtf
echo $object['22']      . '<br>'; // wtf    
?>

I get this as result:

Array           ( [key] => value [1] => value [11] => value [22] => value ) 
stdClass Object ( [key] => value [1] => value [11] => value [22] => value ) 

value
Notice: Undefined property: stdClass::$1 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 19
Notice: Undefined property: stdClass::$11 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 20
Notice: Undefined property: stdClass::$22 in C:\Users\wl\Documents\USBWebserver v8.5\USBWebserver v8.5\8.5\root\Client\test.php on line 21


stdClass Object ( [key] => changed value [1] => value [11] => value [22] => value [1] => changed value [11] => changed value [22] => changed value ) 

changed value
changed value
changed value
changed value

Array ( [key] => changed value [1] => value [11] => value [22] => value [1] => changed value [11] => changed value [22] => changed value ) 

changed value
value
value
value

Is this a bug in PHP why is this happening. How can A array Or object have 2 equal keys with different values!?

1 Answer 1

5

Update:

NikiC provided me an answer, it's explained here: The Symtable

Basically, arrays store numeric keys as integers, objects store numeric keys as strings. But when you do an array-to-object or object-to-array cast that constraint is not enforced.

array (size=7)
  'key' => string 'changed value' (length=13)
  1 => string 'value' (length=5)
  11 => string 'value' (length=5)
  22 => string 'value' (length=5)
  '1' => string 'changed value' (length=13)
  '11' => string 'changed value' (length=13)
  '22' => string 'changed value' (length=13)

That's not correct; e.g. 11 is integer and '11' is a string (two different keys).

Use var_dump instead of print_r, as it shows type.

I suggest you try with this script:

<?php

$object = array(
  'key' => 'value',
  1 => 'value',
  '11' => 'value',
  '22' => 'value'
);

$object = (object) $object;

var_dump($object);

$object->key = 'changed value';
$object->{1} = 'changed value';
$object->{'11'} = 'changed value';
$object->{'22'} = 'changed value';

$object = (array) $object;

var_dump($object);

There are certainly some oddities here, but you can't expect to get good results because variables starting with integer are illegal.

Sign up to request clarification or add additional context in comments.

3 Comments

I did not claim that this was oop, but fine. This php behavior is really unsuspected. Why is this not generating a fatal error. How can a array still contain object properties. And of course the other way around. Object with array values. Its just a php wtf.
@sirwilliam wtf or not, look here 3v4l.org/V6lD1#v500, it sort of makes sense. wasn't trying to offend, sorry.
@sirwilliam I've updated the answer with answer I got from PHP core engineer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.