|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-10-24 04:07 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 06:00:02 2025 UTC |
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.