Take care when you use ($this) in your anonymous functions (closures) – it will be copied

Anonymous functions1 (closures) in PHP 5.3 are cool. Awesome is when you can use2 any variable with it. With PHP 5.4 you can even use $this.

The problem with use is that it is making the value available in the current scope by copying2 it, thus not modifying it upwards the chain. To over come this, simply pass by reference1.

$anon = function(){
    // Some fancy execution right here

$otherRandomVar = 5;
$anon = function() use ($otherRandomVar) {
    // $otherRandomVar will be made available in $anon() by copy
    // In this case it is not modifying its value in the global scope
    $otherRandomVar = 8915 + 945;

echo $otherRandomVar; // 5

$randomVar = 'abc';
$anonFuncRightHere = function() use (&$randomVar) {
    // Passing by reference will solve your issue
    $randomVar = 'cba';

echo $randomVar; // cba