Open In App

PHP class_parents() Function

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The class_parents() function is an inbuilt function in PHP where the parent class for the given class will be returned.

Syntax:

class_parents(
    object|string $object_or_class, 
    bool $autoload = true
): array|false

Parameters: This function accepts two parameters that are described below:

  • object_or_class: This parameter specifies the object(i.e. a class instance) or a string that has the class name.
  • autoload: This parameter, by default, defines & calls the __autoload

Return Value: If the function is successful then it will return an array otherwise it will return “false”.

Example 1:  The code demonstrates the class_parents() functions

PHP




<?php
  
class GFG {}
class Article extends GFG{} {
    print_r(class_parents(new Article));
}
  
?>


Output

Array
(
    [GFG] => GFG
)

Example 2: The code demonstrates the class_parents() function.

PHP




<?php
  
class GFG2 {}
class Article extends GFG2{} {}
$ob = new Article();
  
print_r(class_parents($ob));  
  
?>


Output

Array
(
    [GFG2] => GFG2
)

Reference: https://www.php.net/manual/en/function.class-parents.php


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads