2015-05-20 9 views

risposta

3

È necessario il use costrutto poi a fare il variabile disponibile/visibile all'interno della funzione:

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

È possibile passare anche più di una sola variabile responsabile con questo. Basta separare altre variabili con una virgola , come ... 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

uso "uso", che consentono di utilizzare una variabile come globale nel campo di applicazione:

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