php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70778 Type hinting for objects
Submitted: 2015-10-24 04:07 UTC Modified: 2016-03-26 21:47 UTC
Votes:4
Avg. Score:4.8 ± 0.4
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:2 (66.7%)
From: flavius@php.net Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.0.0RC5 OS: ArchLinux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: flavius@php.net
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-24 04:27 UTC] 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-29 09:23 UTC] 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-11-26 19:49 UTC] webmaster at tubo-world dot de
\stdClass is already supported as valid typehint for stdClass objects! So it cannot be extended for every possible object.
 [2015-12-19 04:00 UTC] ajf@php.net
-Type: Bug +Type: Feature/Change Request
 [2016-03-26 21:47 UTC] krakjoe@php.net
-Status: Open +Status: Suspended
 [2016-03-26 21:47 UTC] krakjoe@php.net
For this kind of change, an RFC is required.

Please see: https://wiki.php.net/rfc/howto
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jun 07 16:01:28 2025 UTC