当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP ReflectionClass getProperties()用法及代码示例


ReflectionClass::getProperties()函数是PHP中的内置函数,用于返回反射属性的数组。

用法:

ReflectionClass::getProperties($filter) : array

参数:此函数接受一个参数过滤器,该过滤器有助于除去某些反射的属性。


返回值:此函数返回反映的属性的数组。

以下示例程序旨在说明PHP中的ReflectionClass::getProperties()函数:
示例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); 
?>

输出:

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:

<?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); 
?>
输出:
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"
  }
}

参考: https://www.php.net/manual/en/reflectionclass.getproperties.php



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 PHP | ReflectionClass getProperties() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。