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

PHP search array in array

Sometimes PHP can be such an old fart. Ever wanted to search for $haystack in an $array which is an array in another array without writing more than one line of code?

One liner for a simple $array – classic:

$bool = in_array($anything, $array);

One liner for multidimensional array for PHP >= 5.5.0

$array = array_search($anything, array_column($array, 'column_key'));

One liner for multidimensional array for PHP < 5.5.0: https://github.com/ramsey/array_column

You’ll have to use Composer, good luck! :-)