$obj = new stdClass();
echo gettype($obj); //object
function abc(object $obj) {
return;
}
abc($obj); //Catchable fatal error: Argument 1 passed to abc() must be an instance of object, instance of stdClass given
Why calling abc($obj) triggers error?
Catchable fatal error: Argument 1 passed to abc() must be an instance of object, instance of stdClass given
Since PHP 5 (Jul 2004; Zend Engine 2) type hinting worked only for class and interface names,
array(PHP 5.1; Nov 2005) orcallable(PHP 5.4; Mar 2012). There was no common ancestorobject(like in some other programming languages like C#) in PHP object/type model. What you had to specify wasstdClassAs of PHP 7.2 (Nov 2017) it’s possible to use
objecttype hint exactly as you guessed in your question:Cf. Type declarations in the PHP manual.