PHP Closure::call()

PHP 7 新特性PHP 7 Neue Features

Die Closure::call() von PHP 7 hat eine bessere Leistung, indem sie dynamisch eine Closure-Funktion an eine neue Objektinstanz bindet und sie aufruft.

Beispiele

Beispiele

<?php
class A {
    private
$x = 1;
}

// Definiere eine Closure-Funktion in Versionen vor PHP 7
$getXCB = function() {
    return
$this->x;
};

// Binde die Closure-Funktion an Klasse A
$getX = $getXCB->bindTo(new A, 'A');

echo
$getX();
print(
PHP_EOL);

// PHP 7+ Code
$getX = function() {
    return
$this->x;
};
echo
$getX->call(new A);
?>

Das obige Programm erzeugt die folgende Ausgabe:

1
1

PHP 7 新特性PHP 7 Neue Features

Andere Erweiterungen