search for in the  
<get_object_varsinterface_exists>
Last updated: Thu, 19 May 2005

get_parent_class

(PHP 4, PHP 5)

get_parent_class -- Retrieves the parent class name for object or class

Description

string get_parent_class ( mixed obj )

If obj is an object, returns the name of the parent class of the class of which obj is an instance.

If obj is a string, returns the name of the parent class of the class with that name. This functionality was added in PHP 4.0.5.

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() and is_subclass_of().



User Contributed Notes
get_parent_class
matt-php at DONT-SPAM-ME dot bitdifferent dot com
01-Nov-2004 09:52
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 at ines dot ro
07-Apr-2004 08:44
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 at correctclick dot com
05-Apr-2003 09:48
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.
eric dot brison at anakeen dot com
28-Jan-2002 06: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
)

<get_object_varsinterface_exists>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT