ReflectionClass::getProperties()函数是PHP中的内置函数,用于返回反射属性的数组。
用法:
ReflectionClass::getProperties($filter) : array
参数:此函数接受一个参数过滤器,该过滤器有助于除去某些反射的属性。
返回值:此函数返回反映的属性的数组。
以下示例程序旨在说明PHP中的ReflectionClass::getProperties()函数:
示例1:
// 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);
?>
输出:
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"
}
}
示例2:
// 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);
?>
输出:
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"
}
}