PHP - Class/Object get_parent_class() Function



The PHP Class/Object get_parent_class() function is used to return the name of the parent class for a particular object or class. It determines the superclass from which the class or object inherits and returns false if no parent class exists. This is useful for determining the inheritance hierarchy of a class.

Syntax

Below is the syntax of the PHP Class/Object get_parent_class() function −

string get_parent_class ( object|string $object = ? )

Parameters

This function accepts $object parameter which is the tested object or class name.

Return Value

The get_parent_class() function returns an array of the names of the declared classes in the current script. If the object does not have a parent or the class does not exist, FALSE is returned.

PHP Version

First introduced in core PHP 4, the get_parent_class() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This example shows a simple class inheritance where Son class extends Father class. The PHP Class/Object get_parent_class() function returns "Father" class.

<?php
   // Create parent class
   class Father {}
   
   // Create child class
   class Son extends Father {}
   
   // Create an object
   $child = new Son();
   
   // Get parent class
   echo "Parent class is: ".get_parent_class($child); 
?>

Output

Here is the outcome of the following code −

Parent class is: Father

Example 2

Let us see what happens after calling the get_parent_class() function when there is no parent class. As SimpleClass does not extend any other class so the function returns false.

<?php
   // Create a simple class
   class SimpleClass {}

   $simplecls = new SimpleClass();

   //Display the result
   echo get_parent_class($simplecls); 
?> 

Output

This will generate the below output −

(bool) false

Example 3

This example shows how we can retrieve the parent class name in a multi-level inheritance case while using the get_parent_class() function.

<?php
   class GrandparentClass {}
   class ParentClass extends GrandparentClass {}
   class ChildClass extends ParentClass {}
   
   // Initialize an object
   $child = new ChildClass();

   //Display the result
   echo get_parent_class($child); 
?> 

Output

This will create the below output −

ParentClass

Example 4

This code shows how to use the get_parent_class() function to get and display the parent class name from child classes. Two child classes inherit from a single parent class, and their constructor display a message about their parent class.

<?php
   class Animal {
      function __construct() {
      }
   }
   
   class Dog extends Animal {
      function __construct() {
          echo "I'm " , get_parent_class($this) , "'s best friend \n";
      }
   }
   
   class Cat extends Animal {
      function __construct() {
          echo "I'm " , get_parent_class('Cat') , "'s independent companion \n";
      }
   }
   
   $dog = new Dog();      
   $cat = new Cat();      
?> 

Output

Following is the output of the above code −

I'm Animal's best friend 
I'm Animal's independent companion 
php_function_reference.htm
Advertisements