(PHP 4, PHP 5)
get_parent_class — Gibt den Namen der Elternklasse eines Objektes zurück
Gibt den Namen der Elternklasse eines Objekts oder einer Klasse zurück.
Das untersuchte Objekt oder der untersuchte Klassenname.
Gibt den Namen der Elternklasse der Klasse zurück, von der object eine Instanz oder der Name ist.
Hinweis:
Falls das Objekt keine Elternklasse hat, wird FALSE zurückgegeben.
Falls die Funktion außerhalb eines Objekts ohne Parameter aufgerufen wird, gibt sie FALSE zurück.
Version | Beschreibung |
---|---|
Vor 5.1.0 | Falls die Funktion außerhalb eines Objekts ohne Parameter aufgerufen wird, gibt sie NULL zurück mit einer Warnung. |
Seit 5.0.0 | Der Parameter object ist optional, falls die Funktion von einer Methode eines Objekts aufgerufen wird. |
Seit 4.0.5 | Falls object eine Zeichenkette ist, gibt diese Funktion den Namen der Elternklasse der Klasse mit diesem Namen zurück. |
Beispiel #1 Die Verwendung von get_parent_class()
<?php
class vater {
function vater()
{
// implementiert etwas Logik
}
}
class kind extends vater {
function kind()
{
echo "Ich bin das Kind von " , get_parent_class($this) , "\n";
}
}
class kind2 extends vater {
function kind2()
{
echo "Ich bin ebenfalls das Kind von " , get_parent_class('kind2') , "\n";
}
}
$foo = new kind();
$bar = new kind2();
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Ich bin das Kind von vater Ich bin ebenfalls das Kind von vater
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;
/*Do not use $this. Use 'self' here instead, or you
* will get an infinite loop. */
$plist = self::getParents($parent, $plist);
}
return $plist;
}
}
class Middle extends Top {
}
class Bottom extends Middle {
}
$o = new Bottom();
print_r($o->getParents());
?>
"'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.)
"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.
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
?>
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
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.
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.
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
)