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
class Company {
protected function GeeksforGeeks( $name ) {
return 'GFG' . $name ;
}
}
$A = new ReflectionMethod( 'Company' , 'GeeksforGeeks' );
$B = $A ->getClosure( new Company());
var_dump( $B );
?>
|
Output:
object(Closure)#3 (2) {
["this"]=>
object(Company)#2 (0) {
}
["parameter"]=>
array(1) {
["$name"]=>
string(10) "<required>"
}
}
Program_2:
PHP
<?php
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 ;
}
}
$A = new ReflectionMethod( 'Department1' , 'HR' );
$B = new ReflectionMethod( 'Department2' , 'Coding' );
$C = new ReflectionMethod( 'Department3' , 'Marketing' );
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...