2015-05-20 18 views

Trả lời

3

Bạn cần use xây dựng sau đó để làm cho có sẵn/có thể nhìn thấy bên trong hàm:

public function fetchWhere($params) { 
    $resultSet = $this->select(function(Select $select) use($params) { 
     $select->where($params); 
    }); 
} 

Bạn có thể chuyển nhiều hơn chỉ một biến với điều này. Chỉ cần tách các biến khác bằng dấu phẩy , như ... use($param1, $param2, ...) {.

2

Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct. It is because of variable scope. Try with -

$resultSet = $this->select(function(Select $select) use($params) { 
    $select->where($params); 
}); 

Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from).

1

sử dụng "sử dụng", cho phép bạn sử dụng một biến như một toàn cầu trong phạm vi:

public function fetchWhere($params) { 
    $resultSet = $this->select(function(Select $select) use($params) { 
    $select->where($params); 
    }); 
.. 
Các vấn đề liên quan