php reflectionmethod,PHP ReflectionMethod getClosure()用法及代码示例

ReflectionMethod::getClosure()函数是PHP中的一个内置函数,用于为该方法返回动态创建的闭包,否则,在出现错误的情况下返回NULL。

用法:

Closure ReflectionMethod::getClosure ( $object )

参数:该函数接受参数对象,该参数对象是指定的user-defiend类对象。

返回值:该函数为该方法返回一个动态创建的闭包,否则,在发生错误的情况下返回NULL。

以下示例程序旨在说明PHP中的ReflectionMethod::getClosure()函数:

程序_1:

// Initializing a user-defined class

class Company {

protected function GeeksforGeeks($name) {

return 'GFG' . $name;

}

}

// Using ReflectionMethod() over the class Company

$A = new ReflectionMethod('Company', 'GeeksforGeeks');

// Calling the getClosure() function

$B = $A->getClosure(new Company());

// Getting a dynamically created closure

// for the method otherwise, return NULL

// in case of an error.

var_dump($B);

?>

输出:

object(Closure)#3 (2) {

["this"]=>

object(Company)#2 (0) {

}

["parameter"]=>

array(1) {

["$name"]=>

string(10) ""

}

}

程序_2:

// Initializing some user-defined classes

class Department1 {

protected function HR($name) {

return 'hr' . $name;

}

}

class Department2 {

protected function Coding($name) {

return 'coding' . $name;

}

}

class Department3 {

protected function Marketing($name) {

return 'marketing' . $name;

}

}

// Using ReflectionMethod() over the above classes

$A = new ReflectionMethod('Department1', 'HR');

$B = new ReflectionMethod('Department2', 'Coding');

$C = new ReflectionMethod('Department3', 'Marketing');

// Calling the getClosure() function and

// Getting a dynamically created closure

// for the method otherwise, return NULL

// in case of an error.

var_dump($A->getClosure(new Department1()));

var_dump($B->getClosure(new Department2()));

var_dump($C->getClosure(new Department3()));

?>

输出:

object(Closure)#5 (2) {

["this"]=>

object(Department1)#4 (0) {

}

["parameter"]=>

array(1) {

["$name"]=>

string(10) ""

}

}

object(Closure)#4 (2) {

["this"]=>

object(Department2)#5 (0) {

}

["parameter"]=>

array(1) {

["$name"]=>

string(10) ""

}

}

object(Closure)#5 (2) {

["this"]=>

object(Department3)#4 (0) {

}

["parameter"]=>

array(1) {

["$name"]=>

string(10) ""

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值