略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: ReflectionClass::isSubclassOf

2024-12-22

ReflectionClass::isSubclassOf

(PHP 5, PHP 7, PHP 8)

ReflectionClass::isSubclassOf检查是否为一个子类

说明

public ReflectionClass::isSubclassOf(string $class): bool

检查一个类是否为指定类的子类,或者实现了指定的接口。

参数

class

被检查的类名。

返回值

成功时返回 true, 或者在失败时返回 false

参见

add a noteadd a note

User Contributed Notes 2 notes

up
2
dhairya lakhera
6 years ago
class A {}
class B {}
class C extends B {}

$obj=new ReflectionClass('C');

var_dump($obj->isSubclassOf ('A')); //boolean false
var_dump($obj->isSubclassOf ('B')); //boolean true
up
1
voitcus at gmail dot com
2 years ago
Note, that this method is a bit different than the `instanceof` operator, which returns true, when it is a subclass or the very same class (interface). Here, only being a subclass results in true, eg.

class A {}
class B extends A {}

$a = new ReflectionClass('A');
$AA = new A;
$b = new ReflectionClass('B');
$BB = new B;

var_dump($a->isSubclassOf($b)); // false
var_dump($AA instanceof $BB); // false

var_dump($b->isSubclassOf($a)); // true
var_dump($BB instanceof $AA); // true

var_dump($a->isSubclassOf($a)); // false
var_dump($AA instanceof $AA); // true

官方地址:https://www.php.net/manual/en/reflectionclass.issubclassof.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3