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) ""
}
}
總結
以上是生活随笔為你收集整理的php reflectionmethod,PHP ReflectionMethod getClosure()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京环球影城可以带食物吗
- 下一篇: 硬包中华多少钱啊?