Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Class/Object Functions : get_parent_class

get_parent_class

Retrieves the parent class name for object or class (PHP 4, PHP 5)
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.

If called without parameter outside object, this function returns FALSE.

ChangeLog

Version Description
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.

Examples

Example 381. 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


Related Examples ( Source code ) » get_parent_class


Code Examples / Notes » get_parent_class

eric dot brison

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
)


birkholz

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
?>


matt-php

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


radu dot rendec

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.


tim

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.


Change Language


Follow Navioo On Twitter
call_user_method_array
call_user_method
class_exists
get_class_methods
get_class_vars
get_class
get_declared_classes
get_declared_interfaces
get_object_vars
get_parent_class
interface_exists
is_a
is_subclass_of
method_exists
property_exists
eXTReMe Tracker