Possible Duplicate:
Is it possible to access outer local variable in PHP?
PHP closure scope problem
给定这个 PHP 函数:
function get_deals_by_type($records, $type) {
$available = function($record) {
if($record->mobile_type == $type) return $record;
};
return array_filter($records, $available);
}
...我如何访问传入的$type
$available
中声明的函数内部?就目前情况而言,$type
返回NULL
对于 array_filter
无论传入 get_deals_by_type()
的值是什么.
最佳答案
不确定,但是:
function get_deals_by_type($records, $type) {
$available = function($record) use ($type) {
if($record->mobile_type == $type) return $record;
};
return array_filter($records, $available);
}
参见http://www.php.net/manual/de/functions.anonymous.php (购物车示例)
关于php - 如何访问声明为变量的函数内的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8541699/