$hooks = function($meta, $type) use ($inspector, $instance) { if (isset($meta[$type])) { $run = array(); foreach ($meta[$type] as $method) { $hookMeta = $inspector->getMethodMeta($method); if (in_array($method, $run) && !empty($hookMeta["@once"])) { continue; } $instance->$method(); $run[] = $method; } } }; $hooks($methodMeta, "@before");
這個hooks function裡面有一個use是怎麼回事?
想不通。。。。望大師告知
回複內容:
$hooks = function($meta, $type) use ($inspector, $instance) { if (isset($meta[$type])) { $run = array(); foreach ($meta[$type] as $method) { $hookMeta = $inspector->getMethodMeta($method); if (in_array($method, $run) && !empty($hookMeta["@once"])) { continue; } $instance->$method(); $run[] = $method; } } }; $hooks($methodMeta, "@before");
這個hooks function裡面有一個use是怎麼回事?
想不通。。。。望大師告知
5.3 新增的閉包文法中,use 用來捕捉變數到匿名函數內,見:
http://www.php.net/manual/zh/functions.anonymous.php
http://www.cnblogs.com/melonblog/archive/2013/05/01/3052611.html