note 123490 added to function.settype

From: Date: Fri, 04 Jan 2019 13:41:56 +0000
Subject: note 123490 added to function.settype
Groups: php.notes 
Request: Send a blank email to php-notes+get-209995@lists.php.net to get a copy of this message
If you attempt to convert the special $this variable from an instance method (only in classes) : * PHP will silently return TRUE and leave $this unchanged if the type was 'bool', 'array', 'object' or 'NULL' * PHP will generate an E_NOTICE if the type was 'int', 'float' or 'double', and $this will not be casted * PHP will throw a catchable fatal error when the type is 'string' and the class does not define the __toString() method Unless the new variable type passed as the second argument is invalid, settype() will return TRUE. In all cases the object will remain unchanged. <?php // This was tested with PHP 7.2 class Foo { function test() { printf("%-20s %-20s %s\n", 'Type', 'Succeed?', 'Converted'); // settype() should throw a fatal error, as $this cannot be re-assigned printf("%-20s %-20s %s\n", 'bool', settype($this, 'bool'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'int', settype($this, 'int'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'float', settype($this, 'float'), print_r($this)); printf("%-20s %-20s %s\n", 'array', settype($this, 'array'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'object', settype($this, 'object'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'unknowntype', settype($this, 'unknowntype'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'NULL', settype($this, 'NULL'), print_r($this, TRUE)); printf("%-20s %-20s %s\n", 'string', settype($this, 'string'), print_r($this, TRUE)); } } $a = new Foo(); $a->test(); ?> Here is the result : Type Succeed? Converted bool 1 Foo Object ( ) Notice: Object of class Foo could not be converted to int in C:\php\examples\oop-settype-this.php on line 9 int 1 Foo Object ( ) Notice: Object of class Foo could not be converted to float in C:\php\examples\oop-settype-this.php on line 10 float 1 Foo Object ( ) array 1 Foo Object ( ) object 1 Foo Object ( ) Warning: settype(): Invalid type in C:\php\examples\oop-settype-this.php on line 14 unknowntype Foo Object ( ) NULL 1 Foo Object ( ) Catchable fatal error: Object of class Foo could not be converted to string in C:\php\examples\oop-settype-this.php on line 15 If the class Foo implements __toString() : <?php class Foo { // ... function __toString() { return 'Foo object is awesome!'; } // ... } ?> So the first code snippet will not generate an E_RECOVERABLE_ERROR, but instead print the same string as for the other types, and not look at the one returned by the __toString() method. Hope this helps ! :) ---- Server IP: 208.43.231.9 Probable Submitter: 2a02:cb43:e::301 (proxied: 2a01:e35:243a:dc10:f140:8838:475e:39ac) ---- Manual Page -- http://php.net/manual/en/function.settype.php Edit -- https://master.php.net/note/edit/123490 Del: integrated -- https://master.php.net/note/delete/123490/integrated Del: useless -- https://master.php.net/note/delete/123490/useless Del: bad code -- https://master.php.net/note/delete/123490/bad+code Del: spam -- https://master.php.net/note/delete/123490/spam Del: non-english -- https://master.php.net/note/delete/123490/non-english Del: in docs -- https://master.php.net/note/delete/123490/in+docs Del: other reasons-- https://master.php.net/note/delete/123490 Reject -- https://master.php.net/note/reject/123490 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#209995) next »