Open In App

PHP class_parents() Function

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:

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
  
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
  
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

Article Tags :