PHP | ReflectionClass getProperties() Function

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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




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.