PHP | ReflectionClass getProperties() Function
Last Updated :
30 Nov, 2019
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
class Departments {
public $Dept1 = 'CSE' ;
private $Dept2 = 'ECE' ;
public static $Dept3 = 'EE' ;
}
$ReflectionClass = new ReflectionClass( 'Departments' );
$A = $ReflectionClass ->getProperties(ReflectionProperty::IS_PUBLIC);
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
class Company {
public $C1 ;
private $C2 ;
public static $C3 ;
}
$ReflectionClass = new ReflectionClass( 'Company' );
$A = $ReflectionClass ->getProperties();
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
Share your thoughts in the comments
Please Login to comment...