Open In App

PHP | ReflectionClass getDefaultProperties() Function

Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?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());
?>


Output:

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

Program 2:




<?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());
?>


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



Last Updated : 30 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads