2015-05-20 18 views

Odpowiedz

3

Musisz use konstrukt następnie, aby zmienna dostępna/widoczna w funkcji:

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

Można przekazać nawet więcej niż jeden zestaw z tym związane. Po prostu oddziel inne zmienne przecinkiem ,, takim jak ... 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

stosowanie "użytkowania", które pozwalają wykorzystać zmienną jako globalną w zakresie:

public function fetchWhere($params) { 
    $resultSet = $this->select(function(Select $select) use($params) { 
    $select->where($params); 
    }); 
.. 
Powiązane problemy