get_parent_class

(PHP 4, PHP 5)

get_parent_classRetrieves the parent class name for object or class

Description

string get_parent_class ([ mixed $object ] )

Retrieves the parent class name for object or class.

Parameters

object

The tested object or class name

Return Values

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.

Changelog

Version Description
5.1.0 If called without parameter outside object, this function would have returned NULL with a warning, but now returns FALSE.
5.0.0 The object parameter is optional if called from the object's method.
4.0.5 If object is a string, returns the name of the parent class of the class with that name.

Examples

Example #1 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();

?>

The above example will output:

I'm dad's son
I'm dad's son too

See Also

  • get_class() - Returns the name of the class of an object
  • is_subclass_of() - Checks if the object has this class as one of its parents

Коментарии

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.
2004-04-07 09:44:41
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
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
2004-11-01 09:52:01
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
"'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.)
2008-05-14 11:32:36
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
Автор:
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 
{
    function 
someFunction() {
        return 
get_child($this__CLASS__);
    }
}

class 
extends {

}

class 
extends {

}

$c = new C();
echo 
$c->someFunction(); //displays B

?>
2011-04-20 11:36:22
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
You can use this function to find common parent of multiple objects or classes.

<?php
/**
 * Returns name of the first (in class hierarchy) common parent class of all provided objects or classes.
 * Returns FALSE when common class is not found.
 *
 * @param mixed $objects Array that can contain objects or class names.
 * @return mixed
 */
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 {
}

    class 
extends {
    }
   
        class 
extends {
        }
       
        class 
extends {
        }

    class 
extends {
    }

        class 
extends {
        }
   
            class 
extends {
            }

class 
{
}

//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'));
?>
2012-04-13 13:27:12
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
An output of the entire inheritance chain using closures, recursion, and OOP

class ParentClass {
    public static function getChain() {
        $chain = null;
        return $function = function($className='') use (& $chain, & $function) {
            if (empty($className))
                $className = static::class;

            if (empty($chain))
                $chain = $className;

            $parent = get_parent_class($className);

            if ($parent !== false) {
                $chain .= " > {$parent}";
                return $function($parent);
            }

            return $chain;
        };
    }
}

class Child extends ParentClass {}
class SubChild extends Child {}
class Sub2 extends SubChild {}
class Sub3 extends Sub2 {}
class Sub4 extends Sub3 {}
class Sub5 extends Sub4 {}
class Sub6 extends Sub5 {}
class Sub7 extends Sub6 {}

printf("%s\n", Sub7::getChain()());

$getChain = Sub7::getChain();
printf("%s\n", $getChain('Sub3'));

Output is:
Sub7 > Sub6 > Sub5 > Sub4 > Sub3 > Sub2 > SubChild > Child > ParentClass
Sub3 > Sub2 > SubChild > Child > ParentClass
2018-11-24 21:28:46
http://php5.kiev.ua/manual/ru/function.get-parent-class.html
Note that from PHP 5.5 you can also use `parent::class` from within a method, e.g.

<?php
   
function child()
    {
        echo 
"I'm "parent::class, "'s son\n";
    }
?>

Looks a bit tidier and technically probably more optimal, as it avoids a function call lookup.
2019-10-18 01:47:58
http://php5.kiev.ua/manual/ru/function.get-parent-class.html

    Поддержать сайт на родительском проекте КГБ