1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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'); |
代码输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/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) |