note 97421 added to function.settype

From: Date: Mon, 19 Apr 2010 06:59:48 +0000
Subject: note 97421 added to function.settype
Groups: php.notes 
Request: Send a blank email to php-notes+get-168390@lists.php.net to get a copy of this message
settype() has some really strange, potentially buggy behavior. As noted by Michael Benedict, using settype() on a variable will initialize that variable. What is stranger is that using settype() on an uninitialized variable that you are treating as an array or object will also initialize the variable. So: <?php settype($foo->bar,"integer"); // stdClass Object ( [test] => 0 ) ?> This works for a chain of any length: $foo->bar['baz']->etc Next we look at what happens if $foo is already set. <?php $foo = false; settype($foo->bar,"integer"); // stdClass Object ( [test] => 0 ) ?> In and of itself, this wouldn't be problematic. It might even make sense. But in all other cases where $foo is defined, even if (boolean) $foo === false, it will throw an error unless $foo->bar is valid (i.e. $foo is an object already). <?php $foo = true; settype($foo->bar,"integer"); // Notice: Trying to get property of non-object ?> ---- Server IP: 69.147.83.197 Probable Submitter: 174.24.158.82 ---- Manual Page -- http://www.php.net/manual/en/function.settype.php Edit -- https://master.php.net/note/edit/97421 Del: integrated -- https://master.php.net/note/delete/97421/integrated Del: useless -- https://master.php.net/note/delete/97421/useless Del: bad code -- https://master.php.net/note/delete/97421/bad+code Del: spam -- https://master.php.net/note/delete/97421/spam Del: non-english -- https://master.php.net/note/delete/97421/non-english Del: in docs -- https://master.php.net/note/delete/97421/in+docs Del: other reasons-- https://master.php.net/note/delete/97421 Reject -- https://master.php.net/note/reject/97421 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#168390) next »