2011-12-17 3 views
2

가능한 중복 :이 PHP 기능을 감안할 때
Is it possible to access outer local variable in PHP?
PHP closure scope problem변수로 선언 된 함수 내에서 변수에 액세스하는 방법은 무엇입니까?

:

function get_deals_by_type($records, $type) { 
    $available = function($record) { 
    if($record->mobile_type == $type) return $record; 
    }; 
    return array_filter($records, $available); 
} 

가 ... 어떻게이 함수의 내부 $type 전달에 액세스 할 수 있습니다 $available에 선언 되었습니까? 현재 의미대로 $typeget_deals_by_type()으로 전달되는 값과 관계없이 array_filter에 대해 NULL을 반환합니다.

답변

4

확실하지만 :

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 (쇼핑 카트 예)

+0

예! 정확히 내가 무엇을 찾고 있었는지. 감사! – neezer

관련 문제