Wednesday, November 20, 2024
Google search engine
HomeLanguagesPHP | ReflectionClass getDefaultProperties() Function

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:




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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments