PHP 闭包函数的理解1

  • Post author:
  • Post published:8月 13, 2019
  • Post category:PHP
  • Post comments:0评论
<?php
class Container {
	public $bindings = [];
	public function bind($a,$b) {
        $closure = $this->getClosure($a,$b);
        var_dump($closure);
        $this->bindings[$a] = $closure;
	}
	public function getClosure($a,$b) {
		return function ($x,$y) use ($a, $b){
			return $x + $y;
		};
	}
}

class App extends Container{

}

$app = new App();
$app->bind('key','value');

代码输出

/var/www/closure/index.php:6:
object(Closure)[2]
  public 'static' => # use 的实参列表,格式为数组,参数变量名为数组的KEY,参数值为KEY 对应的 值
    array (size=2)
      'a' => string 'key' (length=3)
      'b' => string 'value' (length=5)
  public 'this' => # 默认绑定对象为调用对象
    object(App)[1]
      public 'bindings' => 
        array (size=0)
          empty
  public 'parameter' => # 参数列表
    array (size=2)
      '$x' => string '<required>' (length=10)
      '$y' => string '<required>' (length=10)

发表评论