$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
为什么调用 abc($obj)
会触发错误?
Catchable fatal error: Argument 1 passed to abc() must be an instance of object, instance of stdClass given
最佳答案
因为类型提示仅适用于类名、接口(interface)名或数组
。在 php 对象模型中没有共同的祖先 object
(就像在其他一些编程语言中,如 C#)。您必须指定的是 stdClass
截至php 7.2现在可以完全按照您在问题中猜测的方式使用 object
类型提示:
function abc(object $obj) {
return;
}
关于php - stdClass 和类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13287593/