Bug #70778 [Opn]: Type hinting for objects

From: Date: Sat, 24 Oct 2015 04:27:53 +0000
Subject: Bug #70778 [Opn]: Type hinting for objects
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196776@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 Updated by: kalle@php.net 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: 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. Previous Comments: ------------------------------------------------------------------------ [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 (#196776) next »