PHP | ReflectionClass getDefaultProperties() Function

The ReflectionClass::getDefaultProperties() function is an inbuilt function in PHP which is used to return the default properties including inherited properties from a specified class.

Syntax:

ReflectionClass::getDefaultProperties(void) : array

Parameters: This function does not accept any parameter.

Return Value: This function returns an array of default properties. These properties are having space for property’s keys and its values. The key is the name of the property and values are the default value of the property. And it also returns NULL if the property is not having any default values.

Below programs illustrate the ReflectionClass::getDefaultProperties() function in PHP:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Defining a class named as College
class College {
      
    // Defining a protected property
    protected $College_Name = 'IIT Delhi';
}
  
// Defining a sub class Departments of the 
// base class College
class Departments extends College {
    public $Dept1 = 'CSE';
    private $Dept2 = 'ECE';
    public static $Dept3 = 'EE';
}
  
// Using ReflectionClass over sub class Departments
$ReflectionClass = new ReflectionClass('Departments');
  
// Getting an array of the default properties
var_dump($ReflectionClass->getDefaultProperties());
?>

chevron_right


Output:

array(4) {
  ["Dept3"]=>
  string(2) "EE"
  ["Dept1"]=>
  string(3) "CSE"
  ["Dept2"]=>
  string(3) "ECE"
  ["College_Name"]=>
  string(9) "IIT Delhi"
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Defining a class named as College
class College {
      
    // Defining a protected property
    protected $College_Name = 'IIT Delhi';
}
  
// Defining a sub class Departments of the 
// base class College
class Departments extends College {
    public $Dept1;
    private $Dept2;
    public static $Dept3;
}
  
// Using ReflectionClass over sub class Departments
$ReflectionClass = new ReflectionClass('Departments');
  
// Getting an array of the default properties
var_dump($ReflectionClass->getDefaultProperties());
?>

chevron_right


Output:

array(4) {
  ["Dept3"]=>
  NULL
  ["Dept1"]=>
  NULL
  ["Dept2"]=>
  NULL
  ["College_Name"]=>
  string(9) "IIT Delhi"
}

Reference: https://www.php.net/manual/en/reflectionclass.getdefaultproperties.php




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.