PHP | ReflectionMethod getClosure() Function
Last Updated :
19 Aug, 2021
The ReflectionMethod::getClosure() function is an inbuilt function in PHP which is used to return a dynamically created closure for the method otherwise, return NULL in case of an error.
Syntax:
Closure ReflectionMethod::getClosure ( $object )
Parameters: This function accepts a parameter object which is the specified user-defined class object.
Return Value: This function returns a dynamically created closure for the method otherwise, return NULL in case of an error.
Below programs illustrate the ReflectionMethod::getClosure() function in PHP:
Program_1:
PHP
<?php
// 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);
?>
Output: object(Closure)#3 (2) {
["this"]=>
object(Company)#2 (0) {
}
["parameter"]=>
array(1) {
["$name"]=>
string(10) "<required>"
}
}
Program_2:
PHP
<?php
// 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()));
?>
Output: object(Closure)#5 (2) {
["this"]=>
object(Department1)#4 (0) {
}
["parameter"]=>
array(1) {
["$name"]=>
string(10) "<required>"
}
}
object(Closure)#4 (2) {
["this"]=>
object(Department2)#5 (0) {
}
["parameter"]=>
array(1) {
["$name"]=>
string(10) "<required>"
}
}
object(Closure)#5 (2) {
["this"]=>
object(Department3)#4 (0) {
}
["parameter"]=>
array(1) {
["$name"]=>
string(10) "<required>"
}
}
Reference: https://www.php.net/manual/en/reflectionmethod.getclosure.php
Explore
Basics
Array
OOPs & Interfaces
MySQL Database
PHP Advance