百科狗-知识改变命运!

is_subclass_of() - php 类对象函数

百变鹏仔2年前 (2023-11-21)阅读数 17#技术干货
文章标签对象

is_subclass_of()

(PHP 4, PHP 5, PHP 7)

如果此对象是该类的子类,则返回TRUE

说明

is_subclass_of(object $object,string $class_name): bool

如果对象$object所属类是类$class_name的子类,则返回TRUE,否则返回FALSE

Note:

自 PHP 5.0.3 起也可以用一个字符串来指定$object参数(类名)。

Example #1is_subclass_of()例子


以上例程会输出:

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory

参见get_class()、get_parent_class()和is_a()。

参数

$object

A class name or an object instance

$class_name

The class name

$allow_string

If this parameter set to false, string class name as$objectis not allowed. This also prevents from calling autoloader if the class doesn't exist.

返回值

This function returnsTRUEif the object$object, belongs to a class which is a subclass of$class_name,FALSEotherwise.

更新日志

版本说明
5.3.9Added$allow_stringparameter
5.0.3You may also specify the$objectparameter as a string (the name of the class)

范例

Example #2is_subclass_of()example

is_subclass_of() - php 类对象函数


以上例程会输出:

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory

注释

Note:

如果此类不是已知类,使用此函数会使用任何已注册的autoloader。

参见

  • get_class()返回对象的类名
  • get_parent_class()返回对象或类的父类名
  • is_a()如果对象属于该类或该类是此对象的父类则返回 TRUE
  • class_parents()返回指定类的父类。
is_subclass_of() works also with classes between the class of obj and the superclass.
example:

echoes 'true' .
This might be useful to someone, so:
If you're using Autoload, you should be aware that this will attempt to autoload $classname if it isn't already loaded. I discovered this when I had something using is_subclass_of inside an error thrown by autoload, which then recursed until it ran out of memory.
The $allow_string parameter is not very clearly documented. When true, it simply allows the first parameter to be the name of a class, instead of an object whose class we are interested in.
Some usage examples:
class parent_class {
  // objects and methods ...
}
$possible_child_object = new possible_child_class(); // might be an extension of parent_class
$result = is_subclass_of($possible_child_object, 'parent_class'); // valid
$result = is_subclass_of($possible_child_object, 'parent_class', false); // valid
$result = is_subclass_of('possible_child_class', 'parent_class', true); // valid
$result = is_subclass_of('possible_child_class', 'parent_class', false); // not valid
It would appear that is_subclass_of is case insensitive unlike get_class in php5.
i.e.

i run across this while migrating some code from php4 to php5 and the code would only half-the-time break.
If you need something similar to is_subclass_of() to determine if a class implements an interface before instantiating it, use reflection:

bool(true)
For PHP4:


result:
bad 

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}