downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

interface_exists> <get_object_vars
Last updated: Fri, 24 Jul 2009

view this page in

get_parent_class

(PHP 4, PHP 5)

get_parent_class객체나 클래스의 부모 클래스명을 얻습니다

설명

string get_parent_class ([ mixed $object ] )

객체나 클래스의 부모 클래스명을 획득합니다.

인수

object

확인하는 객체나 클래스명

반환값

object 인스턴스나 클래스명의 부모 클래스명을 반환합니다.

Note: 객체에 부모가 존재하지 않으면 FALSE를 반환합니다.

객체 밖에서 인수 없이 호출하면, FALSE를 반환합니다.

변경점

버전 설명
5.1.0 이전 객체가 밖에서 인수 없이 호출하면, NULL을 반환하고 경고를 발생했습니다.
5.0.0부터 객체 메쏘드에서 호출할 때 object 인수가 선택적이 되었습니다.
4.0.5부터 object 가 문자열이면, 그 이름을 가진 클래스의 부모 클래스명을 반환합니다.

예제

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

참고



interface_exists> <get_object_vars
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
get_parent_class
michael at getsprink dot -- com
09-Apr-2009 07:28
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());

?>
ssb45 at cornell dot edu
14-May-2008 03:32
"'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.)
marcus at synchromedia dot co dot uk
16-Apr-2008 04:08
"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.
birkholz at web dot de
07-Oct-2005 12:01
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 at DONT-SPAM-ME dot bitdifferent dot com
01-Nov-2004 03: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 01: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
06-Apr-2003 03: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 12: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
)

interface_exists> <get_object_vars
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites