note 90447 added to language.types.object

From: Date: Thu, 23 Apr 2009 01:21:01 +0000
Subject: note 90447 added to language.types.object
Groups: php.notes 
Request: Send a blank email to php-notes+get-153599@lists.php.net to get a copy of this message
CAUTION: "Arrays convert to an object with properties named by keys, and corresponding values". This is ALWAYS true, which means that even numeric keys are accepted when converting. But the resulting properties cannot be accessed, since they don't match the variables naming rules. So this: <?php $x = (object) array('a'=>'A', 'b'=>'B', 'C'); echo '<pre>'.print_r($x, true).'</pre>'; ?> works and displays: stdClass Object ( [a] => A [b] => B [0] => C ) But this: <?php echo '<br />'.$x->a; echo '<br />'.$x->b; echo '<br />'.$x->{0}; # (don't use $x->0, which is obviously a syntax error) ?> fails and displays: A B Notice: Undefined property: stdClass::$0 in... ---- Server IP: 89.31.146.176 Probable Submitter: 86.200.179.35 ---- Manual Page -- http://www.php.net/manual/en/language.types.object.php Edit -- https://master.php.net/note/edit/90447 Del: integrated -- https://master.php.net/note/delete/90447/integrated Del: useless -- https://master.php.net/note/delete/90447/useless Del: bad code -- https://master.php.net/note/delete/90447/bad+code Del: spam -- https://master.php.net/note/delete/90447/spam Del: non-english -- https://master.php.net/note/delete/90447/non-english Del: in docs -- https://master.php.net/note/delete/90447/in+docs Del: other reasons-- https://master.php.net/note/delete/90447 Reject -- https://master.php.net/note/reject/90447 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#153599) next »