is_subclass_of() - php 类对象函数
is_subclass_of()
(PHP 4, PHP 5, PHP 7)
如果此对象是该类的子类,则返回TRUE
说明
is_subclass_of(object $object,string $class_name): bool如果对象$object所属类是类$class_name的子类,则返回TRUE
,否则返回FALSE
。
自 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()。
参数
$objectA class name or an object instance
$class_nameThe class name
$allow_stringIf 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 returnsTRUE
if the object$object, belongs to a class which is a subclass of$class_name,FALSE
otherwise.
更新日志
版本 | 说明 |
---|---|
5.3.9 | Added$allow_stringparameter |
5.0.3 | You may also specify the$objectparameter as a string (the name of the class) |
范例
Example #2is_subclass_of()example
以上例程会输出:
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()
如果对象属于该类或该类是此对象的父类则返回 TRUEclass_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
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!