©
本文档使用 PHP中文网手册 发布
(PHP 4, PHP 5, PHP 7)
get_parent_class — 返回对象或类的父类名
$obj
] )
如果 obj
是对象,则返回对象实例 obj
所属类的父类名。
如果 obj
是字符串,则返回以此字符串为名的类的父类名。此功能是在
PHP 4.0.5 中增加的。
Note:
自 PHP 5 起,如果在对象的方法内调用,则
obj
为可选项。
Example #1 使用 get_parent_class()
<?php
class dad {
function dad ()
{
// implements some logic
}
}
class child extends dad {
function child ()
{
echo "I'm " , get_parent_class ( $this ) , "'s son\n" ;
}
}
class child2 extends dad {
function child2 ()
{
echo "I'm " , get_parent_class ( 'child2' ) , "'s son too\n" ;
}
}
$foo = new child ();
$bar = new child2 ();
?>
以上例程会输出:
I'm dad's son I'm dad's son too
参见 get_class() 和 is_subclass_of() 。
object
The tested object or class name
Returns the name of the parent class of the class of which
object
is an instance or the name.
Note:
If the object does not have a parent or the class given does not exist
FALSE
will be returned.
If called without parameter outside object, this function returns FALSE
.
版本 | 说明 |
---|---|
Before 5.1.0 |
If called without parameter outside object, this function would have
returned NULL with a warning.
|
Since 5.0.0 |
The object parameter is optional if called
from the object's method.
|
Since 4.0.5 |
If object is a string, returns the name of the
parent class of the class with that name.
|
Example #2 Using get_parent_class()
<?php
class dad {
function dad ()
{
// implements some logic
}
}
class child extends dad {
function child ()
{
echo "I'm " , get_parent_class ( $this ) , "'s son\n" ;
}
}
class child2 extends dad {
function child2 ()
{
echo "I'm " , get_parent_class ( 'child2' ) , "'s son too\n" ;
}
}
$foo = new child ();
$bar = new child2 ();
?>
以上例程会输出:
I'm dad's son I'm dad's son too
[#1] falundir at gmail dot com [2012-04-13 10:27:12]
You can use this function to find common parent of multiple objects or classes.
<?php
function get_first_common_parent($objects) {
$common_ancestors = null;
foreach($objects as $object) {
if (is_object($object)) {
$class_name = get_class($object);
} else {
$class_name = $object;
}
$parent_class_names = array();
$parent_class_name = $class_name;
do {
$parent_class_names[] = $parent_class_name;
} while($parent_class_name = get_parent_class($parent_class_name));
if ($common_ancestors === null) {
$common_ancestors = $parent_class_names;
} else {
$common_ancestors = array_intersect($common_ancestors, $parent_class_names);
}
}
return reset($common_ancestors);
}
?>
Example:
<?php
class A {
}
class B extends A {
}
class D extends B {
}
class E extends B {
}
class C extends A {
}
class F extends C {
}
class G extends F {
}
class H {
}
//returns "A"
get_first_common_parent(array('G', 'E'));
//returns "F"
get_first_common_parent(array(new G(), 'F'));
//returns false (no common parent)
get_first_common_parent(array('C', 'H'));
//returns false (non-existent class provided)
get_first_common_parent(array(new B(), 'X'));
?>
[#2] levu [2011-04-20 08:36:22]
I wrote a simple function doing the reverse thing: get the children:
<?php
function get_child($instance, $classname) {
$class = $classname;
$t = get_class($instance);
while (($p = get_parent_class($t)) !== false) {
if ($p == $class) {
return $t;
}
$t = $p;
}
return false;
}
abstract class A {
function someFunction() {
return get_child($this, __CLASS__);
}
}
class B extends A {
}
class C extends B {
}
$c = new C();
echo $c->someFunction(); //displays B
?>
[#3] michael at getsprink dot -- com [2009-04-09 12:28:48]
This little snippet to get the inheritance tree might be useful to someone.
<?php
header("Content-Type: text/plain;");
class Top {
public function getParents($class=null, $plist=array()) {
$class = $class ? $class : $this;
$parent = get_parent_class($class);
if($parent) {
$plist[] = $parent;
$plist = self::getParents($parent, $plist);
}
return $plist;
}
}
class Middle extends Top {
}
class Bottom extends Middle {
}
$o = new Bottom();
print_r($o->getParents());
?>
[#4] ssb45 at cornell dot edu [2008-05-14 08:32:36]
"'If called without parameter outside object' What on earth does that mean?"
There are two places this could be called:
1. From within a member function of an object. In this case, it may be called with no parameters and will return the parent class of the object owning the member function. (If the parameter is included, then it will return the parent class of the specified class as normal.)
2. From outside an object (i.e., global or function scope). In this case, PHP doesn't know what class you're talking about if you don't include a parameter, so it returns FALSE. (But, of course, it works if you specify the class with the parameter.)
[#5] marcus at synchromedia dot co dot uk [2008-04-16 09:08:26]
"If called without parameter outside object" What on earth does that mean?
What I can tell you, and that is not documented, is that if the object in question does not have an explicitly declared parent class, it does return boolean false. It doesn't for example return 'stdClass' on the basis that all objects are derived from that.
[#6] birkholz at web dot de [2005-10-06 17:01:55]
tim at correctclick dot com wrote:
<quote>
A slightly more cryptic but faster get_ancestors function:
<?php
function get_ancestors ($class) {
for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
return $classes;
}
?>
(The second part of the for is implicitly testing for $class != ""). Recursion is considerably slower than looping, so you probably want to use this function.
Hope someone finds it useful.
</quote>
I would prefer this version, because it will create no duplicates:
<?php
function get_ancestors ($class) {
$classes = array($class);
while($class = get_parent_class($class)) { $classes[] = $class; }
return $classes;
}
Greets, Dennis
?>
[#7] matt-php at DONT-SPAM-ME dot bitdifferent dot com [2004-11-01 07:52:01]
PHP (4 at least, dunno about 5) stores classnames in lower case, so:
<?PHP
class Foo
{
}
class Bar extends Foo
{
}
echo get_parent_class('Bar');
echo "\n";
echo get_parent_class('bar');
?>
will output:
foo
foo
[#8] radu dot rendec at ines dot ro [2004-04-07 06:44:41]
If the argument obj is a string and the class is not defined, then the function returns FALSE.
If the argument obj is an object created from a class with no ancestors (or a string representing a class with no ancestors), then the function returns FALSE.
[#9] tim at correctclick dot com [2003-04-05 19:48:44]
A slightly more cryptic but faster get_ancestors function:
function get_ancestors ($class) {
for ($classes[] = $class; $class = get_parent_class ($class); $classes[] = $class);
return $classes;
}
(The second part of the for is implicitly testing for $class != ""). Recursion is considerably slower than looping, so you probably want to use this function.
Hope someone finds it useful.
[#10] eric dot brison at anakeen dot com [2002-01-28 04:14:14]
To return all ancestors class of an object
function get_ancestors_class($classname) {
$father = get_parent_class($classname);
if ($father != "") {
$ancestors = get_ancestors_class($father);
$ancestors[] = $father;
}
return $ancestors;
}
example :
-----------
Class C {
}
Class B extends C {
}
Class A extends B {
}
print_r (get_ancestors_class("a"));
print_r (get_ancestors_class("b"));
example result :
---------------
Array
(
[0] => c
[1] => b
)
Array
(
[0] => c
)