Computer >> Computer tutorials >  >> Programming >> PHP

get_parent_class() function in PHP


The get_parent_class() retrieves the parent class name for object or class.

Syntax

get_parent_class(object)

Parameters

  • object  − The tested object or name of the class.

Return

The get_parent_class() function returns the name of the parent class of the class.

Example

The following is an example −

<?php
class Demo {
   function Demo() {
   }
}
class Demo2 extends Demo {
   function Demo2() {
      echo "I'm " , get_parent_class($this) , "'s son \n";
   }
}
class Demo3 extends Demo {
   function Demo3() {
      echo "I'm " , get_parent_class('Demo2') , "'s son too \n";
   }
}
$one = new Demo2();
$two = new Demo3();
?>

Output

I'm Demo's son
I'm Demo's son too