©
Ce document utilise Manuel du site Web PHP chinois Libérer
(PHP 5)
ReflectionClass::getParentClass — 获取父类
本函数还未编写文档,仅有参数列表。
此函数没有参数。
一个 ReflectionClass。
[#1] isaac dot z dot foster at gmail dot com [2013-06-15 21:02:10]
Quick correction, the code for getting all parent classes below has a "typo", you need to reset the $class variable to the parent class instance, otherwise it just endlessly loops:
$class = new ReflectionClass('classname');
$parents = array();
while ($parent = $class->getParentClass()) {
$parents[] = $parent->getName();
$class = $parent;
}
echo "Parents: " . implode(", ", $parents);
[#2] havelangep at hotmail dot com [2010-12-16 01:26:38]
Here is a "replacement" for is_a that will additionally look both into the extended classes and in the implemented interfaces
<?php
function IsExtendsOrImplements( $search, $className ) {
$class = new ReflectionClass( $className );
if( false === $class ) {
return false;
}
do {
$name = $class->getName();
if( $search == $name ) {
return true;
}
$interfaces = $class->getInterfaceNames();
if( is_array( $interfaces ) && in_array( $search, $interfaces )) {
return true;
}
$class = $class->getParentClass();
} while( false !== $class );
return false;
}
?>
[#3] jochem at drecomm dot nl [2010-11-18 04:34:59]
When you want to find all parents (parent, parent of parent, parent of parent's parent and so on) try:
<?php
$class = new ReflectionClass('whatever');
$parents = array();
while ($parent = $class->getParentClass()) {
$parents[] = $parent->getName();
}
echo "Parents: " . implode(", ", $parents);
?>
ReflectionClass::getParentClass() can return a ReflectionClass object of the parent class or false if no parent.
(PHP Version 5.1.6)