查看一个对象可以说是其实例的所有类和接口
See all classes and interfaces that an object can be said to be an instance of
有没有一种简单的方法可以获取任何对象并获得所有 类 的完整列表以及与 instanceof
相比 return true
的接口?
这可能不是最好的方法(甚至不是正确的方法),但在尝试自己寻找这个问题的答案时,我想到了这个函数:
function getAllTypes($object) {
$reflection = new ReflectionObject($object);
$types = $reflection->getInterfaceNames();
$types[] = get_class($object);
while($reflection = $reflection->getParentClass()) {
$types[] = $reflection->getName();
}
return $types;
}
然后我用
测试了它
// Fake classes and interfaces
interface Interface1 {};
interface Interface2 {};
interface Interface3 {};
abstract class Abstract1 implements Interface1 {};
class Class1 extends Abstract1 implements Interface2 {}
class Class2 extends Class1 implements Interface3 {}
// Instantiated object
$testObject = new Class2();
// Test instance of
echo $testObject instanceof Class2 ? '.' : 'X';
echo $testObject instanceof Class1 ? '.' : 'X';
echo $testObject instanceof Abstract1 ? '.' : 'X';
echo $testObject instanceof Interface3 ? '.' : 'X';
echo $testObject instanceof Interface2 ? '.' : 'X';
echo $testObject instanceof Interface1 ? '.' : 'X';
echo PHP_EOL;
// Print all Types
$types = getAllTypes($testObject);
foreach($types as $type) {
echo $type.PHP_EOL;
}
运行 在控制台中给出:
$ php test.php
......
Interface2
Interface1
Interface3
Class2
Class1
Abstract1
这是最好的方法吗?我错过了什么吗?
编辑:
不使用反射更简单的方法:
function getAllTypesEasy($object) {
return array_merge(
[get_class($object)],
class_parents($object),
class_implements($object)
);
}
编辑 2:
无需在此处编写所有代码(参见 link),进一步调查显示您 do 需要使用反射如果您想获取用于构建对象的所有特征。此外,HHVM 和 PHP 对结果的排序略有不同:
有没有一种简单的方法可以获取任何对象并获得所有 类 的完整列表以及与 instanceof
相比 return true
的接口?
这可能不是最好的方法(甚至不是正确的方法),但在尝试自己寻找这个问题的答案时,我想到了这个函数:
function getAllTypes($object) {
$reflection = new ReflectionObject($object);
$types = $reflection->getInterfaceNames();
$types[] = get_class($object);
while($reflection = $reflection->getParentClass()) {
$types[] = $reflection->getName();
}
return $types;
}
然后我用
测试了它// Fake classes and interfaces
interface Interface1 {};
interface Interface2 {};
interface Interface3 {};
abstract class Abstract1 implements Interface1 {};
class Class1 extends Abstract1 implements Interface2 {}
class Class2 extends Class1 implements Interface3 {}
// Instantiated object
$testObject = new Class2();
// Test instance of
echo $testObject instanceof Class2 ? '.' : 'X';
echo $testObject instanceof Class1 ? '.' : 'X';
echo $testObject instanceof Abstract1 ? '.' : 'X';
echo $testObject instanceof Interface3 ? '.' : 'X';
echo $testObject instanceof Interface2 ? '.' : 'X';
echo $testObject instanceof Interface1 ? '.' : 'X';
echo PHP_EOL;
// Print all Types
$types = getAllTypes($testObject);
foreach($types as $type) {
echo $type.PHP_EOL;
}
运行 在控制台中给出:
$ php test.php
......
Interface2
Interface1
Interface3
Class2
Class1
Abstract1
这是最好的方法吗?我错过了什么吗?
编辑:
不使用反射更简单的方法:
function getAllTypesEasy($object) {
return array_merge(
[get_class($object)],
class_parents($object),
class_implements($object)
);
}
编辑 2:
无需在此处编写所有代码(参见 link),进一步调查显示您 do 需要使用反射如果您想获取用于构建对象的所有特征。此外,HHVM 和 PHP 对结果的排序略有不同: