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

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"

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值