Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Closure Binding as an alternative to “use” variables

On his blog Mark Baker shares some thoughts on how to use closure binding to avoid having import variables with the use keyword.

You’ll learn how to rewrite

$filteredArrayData = array_filter(
    $arrayData,
    function($value) use ($minimumPrice, $maximumPrice) {
        return $value->price >= $minimumPrice && $value->price < $maximumPrice;
    }
);

to

$filteredArrayData = array_filter(
    $bookData,
    $priceFilter->inRange(5.00, 15.00)
);

https://markbakeruk.net/2017/03/12/closure-binding-as-an-alternative-to-use-variables/

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.