PHP Conference Kansai 2025

ReflectionClass::isSubclassOf

(PHP 5, PHP 7, PHP 8)

ReflectionClass::isSubclassOfChecks if a subclass

Description

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

Checks if the class is a subclass of a specified class or implements a specified interface.

Parameters

class

Either the name of the class as string or a ReflectionClass object of the class to check against.

Return Values

Returns true on success or false on failure.

See Also

add a note

User Contributed Notes 2 notes

up
3
voitcus at gmail dot com
5 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
up
3
dhairya lakhera
9 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
To Top

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant