Thursday, October 23, 2025
HomeLanguagesPHP | ReflectionClass getProperties() Function

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:




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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS