首页 > 解决方案 > 如何在不创建任何一个类的情况下查看一个类是否是另一个类的实例?

问题描述

我有两节课。

我想检查一个类是否是另一个类的实例而不初始化它们中的任何一个。

使用该功能is_subclass_of和使用时,instanceOf您必须初始化子类。

<?php

$classPath = 'app/foo/bar';
$subClassPath = 'app/foo/foo'; // Inherits from $classPath

// This is what I want to do but doesn't work.
echo $subClassPath instanceOf $classPath;

// Works
echo (new $subClassPath()) instanceOf $classPath;

标签: php

解决方案


我发现了问题。

is_subclass_of要求你的斜线走另一条路。

$classPath = 'app\foo\bar';
$subClassPath = 'app\foo\foo'; // Inherits from $classPath

// Works
echo is_subclass_of($subClassPath $classPath);

推荐阅读