【发布时间】:2017-04-12 00:03:36
【问题描述】:
如何将类型传递给函数,以便它可以检查对象是否来自传递的类型。
我的尝试似乎不起作用:
class Test {
function __construct() {
}
}
function check(Type) {
$x = new Test();
if ($x instanceof Type) {
print_r("the object is from the type of the passed variable! :)");
}
}
check(Test);
【问题讨论】:
-
只需将类型名称作为字符串传递。
-
见
get_class()。它不适用于继承,但可能足以满足您的需求。 -
或者使用 php 中的
is_a($object,$classname)函数 php.net/manual/en/function.is-a.php -
感谢您的快速回复,但不,我不想将其作为字符串传递。 Java / C#中没有类似泛型方法的东西吗?
-
not pass it as strint那么呢? , 但是让我们结合is_a($object,get_class($otherwhatver)),如果你真的来自C#,那么你将很难使用php ;-)