📜  PHP | ReflectionMethod getClosure()函数

📅  最后修改于: 2022-05-13 01:56:49.358000             🧑  作者: Mango

PHP | ReflectionMethod getClosure()函数

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

句法:

Closure ReflectionMethod::getClosure ( $object )

参数:该函数接受一个参数对象,该对象是指定的用户定义的类对象。

返回值:此函数为该方法返回一个动态创建的闭包,否则返回 NULL 以防出错。

下面的程序说明了PHP中的ReflectionMethod::getClosure()函数

程序_1:

PHP
getClosure(new Company());
 
// Getting a dynamically created closure
// for the method otherwise, return NULL
// in case of an error.
var_dump($B);
?>


PHP
getClosure(new Department1()));
var_dump($B->getClosure(new Department2()));
var_dump($C->getClosure(new Department3()));
?>


输出:
object(Closure)#3 (2) {
  ["this"]=>
  object(Company)#2 (0) {
  }
  ["parameter"]=>
  array(1) {
    ["$name"]=>
    string(10) ""
  }
}

程序_2:

PHP

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) ""
  }
}

参考: https://www. PHP.net/manual/en/reflectionmethod.getclosure。 PHP