检查一个类是否是另一个的子类 [英] Checking if a class is a subclass of another

查看:189
本文介绍了检查一个类是否是另一个的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个类是否是另一个的子类,而不创建一个实例。我有一个类接收作为参数的类名,作为验证过程的一部分,我想检查它是否是一个特定的类族(以防止安全问题等)。

I want to check if a class is a subclass of another without creating an instance. I have a class that receives as a parameter a class name, and as a part of the validation process, I want to check if it's of a specific class family (to prevent security issues and such). Any good way of doing this?

推荐答案

is_subclass_of() 将正确检查类是否扩展另一个类,但不会返回 true 如果两个参数相同( is_subclass_of('Foo','Foo')将会是 false )。

is_subclass_of() will correctly check if a class extends another class, but will not return true if the two parameters are the same (is_subclass_of('Foo', 'Foo') will be false).

一个简单的平等检查将添加所需的功能。

A simple equality check will add the functionality you require.

function is_class_a($a, $b)
{
    return $a == $b || is_subclass_of($a, $b);
}

这篇关于检查一个类是否是另一个的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆