Bug #70778 [Com]: Type hinting for objects

From: Date: Thu, 26 Nov 2015 19:49:42 +0000
Subject: Bug #70778 [Com]: Type hinting for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197443@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70778&edit=1 ID: 70778 Comment by: webmaster at tubo-world dot de Reported by: flavius@php.net Summary: Type hinting for objects Status: Open Type: Bug Package: Scripting Engine problem Operating System: ArchLinux PHP Version: 7.0.0RC5 Block user comment: N Private report: N New Comment: \stdClass is already supported as valid typehint for stdClass objects! So it cannot be extended for every possible object. Previous Comments: ------------------------------------------------------------------------ [2015-10-29 09:23:55] arjen at react dot com Exactly my thoughts! Was running this snippet of code this week (simplified): Class TestCase { function assertInstanceOf($expectedClass, $actualObject) { if (!$actualObject instanceof $expectedClass) throw new Exception(get_class($actualObject) . " no instanceOf $expectedClass"); } } But with some call $actualObject was null, so the exception message became 'TestCase no instance of $expectedClass'.. After finding out the cause of the unexpected error message, I wanted to add an object typehint to $actualObject.... :-/ So +1 on this FR. ------------------------------------------------------------------------ [2015-10-24 04:27:50] kalle@php.net Seems like something we could look into adding in 7.1 along with maybe the return type void hint that is currently up for discussion on internals@. I would write an RFC for this if I were you and put it up for discussion. ------------------------------------------------------------------------ [2015-10-24 04:07:33] flavius@php.net Description: ------------ There is no way of making sure that a parameter is an object. Since there is a type hint for many is_*() functions, there should be one for objects too - maybe just \stdClass? Test script: --------------- <?php function acceptsObject(\stdClass $o) {} $anonymousObject = new class() {}; acceptsObject(new DOMDocument()); acceptsObject($anonymousObject); Actual result: -------------- PHP Fatal error: Uncaught TypeError: Argument 1 passed to acceptsObject() must be an instance of stdClass, instance of DOMDocument given, called in /tmp/test.php on line 5 and defined in /tmp/test.php:2 Stack trace: #0 /tmp/test.php(5): acceptsObject(Object(DOMDocument)) #1 {main} thrown in /tmp/test.php on line 2 Fatal error: Uncaught TypeError: Argument 1 passed to acceptsObject() must be an instance of stdClass, instance of DOMDocument given, called in /tmp/test.php on line 5 and defined in /tmp/test.php:2 Stack trace: #0 /tmp/test.php(5): acceptsObject(Object(DOMDocument)) #1 {main} thrown in /tmp/test.php on line 2 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70778&edit=1

« previous php.bugs (#197443) next »