Open In App

PHP | ReflectionClass getProperties() Function

Last Updated : 30 Nov, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The ReflectionClass::getProperties() function is an inbuilt function in PHP which is used to return an array of the reflected properties. Syntax:
ReflectionClass::getProperties($filter) : array
Parameters: This function accepts a parameter filter which helps to remove some of the reflected properties. Return Value: This function returns an array of the reflected properties. Below programs illustrate the ReflectionClass::getProperties() function in PHP: Program 1: php
<?php

// Defining a class named as Departments
class Departments {
    public $Dept1 = 'CSE';
    private $Dept2 = 'ECE';
    public static $Dept3 = 'EE';
}

// Using ReflectionClass over the class Departments
$ReflectionClass = new ReflectionClass('Departments');

// Calling getProperties() function over a filter called 
// ReflectionProperty::IS_PUBLIC which  
// will reflect results of only public properties
$A = $ReflectionClass->getProperties(ReflectionProperty::IS_PUBLIC);

// Getting an array of the reflected properties
var_dump($A);
?>
Output:
array(2) {
  [0]=>
  object(ReflectionProperty)#2 (2) {
    ["name"]=>
    string(5) "Dept1"
    ["class"]=>
    string(11) "Departments"
  }
  [1]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(5) "Dept3"
    ["class"]=>
    string(11) "Departments"
  }
}
Program 2: php
<?php
  
// Defining a class named as Company
class Company {
    public $C1;
    private $C2;
    public static $C3;
}
  
// Using ReflectionClass over the class Company
$ReflectionClass = new ReflectionClass('Company');
  
// Calling getProperties() function without
// of any filter
$A = $ReflectionClass->getProperties();
  
// Getting an array of the reflected properties
var_dump($A);
?>
Output:
array(3) {
  [0]=>
  object(ReflectionProperty)#2 (2) {
    ["name"]=>
    string(2) "C1"
    ["class"]=>
    string(7) "Company"
  }
  [1]=>
  object(ReflectionProperty)#3 (2) {
    ["name"]=>
    string(2) "C2"
    ["class"]=>
    string(7) "Company"
  }
  [2]=>
  object(ReflectionProperty)#4 (2) {
    ["name"]=>
    string(2) "C3"
    ["class"]=>
    string(7) "Company"
  }
}
Reference: https://www.php.net/manual/en/reflectionclass.getproperties.php

Next Article

Similar Reads