PHP Function Reference

PHP get_parent_class() Function



The PHP get_parent_class() function is used to retrieve the parent class name for an object or a class.

Syntax

get_parent_class(object_or_class)

Parameters

object_or_class Optional. Specify the class name or an object of the class which need to be checked. This parameter is optional if called from the object's method.

Return Value

Returns the name of the parent class name for the given object or class. If the function is called without parameter outside object, it returns false.

Example: get_parent_class() example

The example below shows the usage of get_parent_class() function.

<?php
class myClass {
  function __construct() {
    //codes
  }
}

class child1Class extends myClass {
  function __construct() {
    echo "parent class of child1Class: ".get_parent_class($this)."\n";
  }
}

class child2Class extends myClass {
  function __construct() {
    echo "parent class of child2Class: ".get_parent_class('child2Class')."\n";
  }
}

class child3Class extends myClass {
  function __construct() {
    //codes
  }
}

$x = new child1Class();
$y = new child2Class();
$z = new child3Class();

echo "parent class of child3Class: ".get_parent_class('child3Class')."\n";
echo "parent class of object z: ".get_parent_class($z)."\n";
?>

The output of the above code will be similar to:

parent class of child1Class: myClass
parent class of child2Class: myClass
parent class of child3Class: myClass
parent class of object z: myClass

❮ PHP Classes/Objects Reference