$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
Because type hinting works only for class name, interface name or array
. There is no common ancestor object
(like in some other programming languages like C#) in php object model. What you have to specify is stdClass
As of php 7.2 it's now possible to use object
type hint exactly as you guessed in your question:
function abc(object $obj) {
return;
}
Read the documentation about type hinting in PHP. Your current code forces abc
function to accept a parameter that is an instance of the class object
(a class named object
!). Do that instead:
function abc(stdClass $obj)