PHP.mk документација

ReflectionMethod::getClosure

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

reflectionmethod.getclosure.php PHP.net прокси Преводот се освежува
Оригинал на PHP.net
Патека reflectionmethod.getclosure.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + превод во позадина Кодовите, табелите и белешките остануваат читливи во истиот тек.
ReflectionMethod::getClosure

Референца за `reflectionmethod.getclosure.php` со подобрена типографија и навигација.

reflectionmethod.getclosure.php

ReflectionMethod::getClosure

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

ReflectionMethod::getClosureReturns a dynamically created closure for the method

= NULL

public ReflectionMethod::getClosure(?object $object = null): Затворање

Create a closure which will call the method.

Параметри

object

Forbidden for static methods, required for other methods.

Вратени вредности

Повикувачот за конвертирање. Затворање.

Errors/Exceptions

Фрла ValueError if object is null but the method is non-static.

Фрла ReflectionException if object is not an instance of the class this method was declared in.

Дневник на промени

Верзија = NULL
8.0.0 object сега е null.

Белешки од корисници 2 забелешки

Denis Doronin
пред 13 години
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->x + $this->y;
    }

}

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

?>

Output is 11.
okto
пред 9 години
Use method from another class context.

<?php

class A {
    private $var = 'class A';

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

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

class B {
    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
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.