ReflectionMethod::getClosure

(PHP >= 5.4.0)

ReflectionMethod::getClosureReturns a dynamically created closure for the method

Description

public Closure ReflectionMethod::getClosure ( object $object )

Warning

This function is currently not documented; only its argument list is available.

Parameters

object

Forbidden for static methods, required for other methods.

Return Values

Returns Closure. Returns NULL in case of an error.

Коментарии

Автор:
You can call private methods with getClosure():

<?php

function call_private_method($object$method$args = array()) {
   
$reflection = new ReflectionClass(get_class($object));
   
$closure $reflection->getMethod($method)->getClosure($object);
    return 
call_user_func_array($closure$args);
}

class 
Example {

    private 
$x 1$y 10;

    private function 
sum() {
        print 
$this->$this->y;
    }

}

call_private_method(new Example(), 'sum');

?>

Output is 11.
2013-01-18 22:05:08
http://php5.kiev.ua/manual/ru/reflectionmethod.getclosure.html
Автор:
Use method from another class context.

<?php

class {
    private 
$var 'class A';

    public function 
getVar() {
        return 
$this->var;
    }

    public function 
getCl() {
        return function () {
           
$this->getVar();
        };
    }
}

class 
{
    private 
$var 'class B';
}

$a = new A();
$b = new B();

print 
$a->getVar() . PHP_EOL;

$reflection = new ReflectionClass(get_class($a));
$closure    $reflection->getMethod('getVar')->getClosure($a);
$get_var_b  $closure->bindTo($b$b);

print 
$get_var_b() . PHP_EOL;

// Output:
// class A
// class B
2016-08-14 13:45:37
http://php5.kiev.ua/manual/ru/reflectionmethod.getclosure.html

    Поддержать сайт на родительском проекте КГБ