PHP Short Functions and Scope
Ross Wintle dissected the recent RFC for auto-capturing multi-statement closures in PHP.
Read more [rosswintle.uk]
Posts tagged with closures
Ross Wintle dissected the recent RFC for auto-capturing multi-statement closures in PHP.
Read more [rosswintle.uk]
Hooks is feature was added recently to React that I really like.
In this article, we reintroduce closures by building a tiny clone of React Hooks. This will serve two purposes – to demonstrate the effective use of closures, and to show how you can build a Hooks clone in just 29 lines of readable JS. Finally, we arrive at how Custom Hooks naturally arise.
Read more [www.netlify.com]
Join 9,500+ smart developers
Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.
No spam. Unsubscribe anytime. You can also follow me on X.
"Freek publishes a super resourceful and practical newsletter. A must for anyone in the Laravel space"
???? Forgot about this little validation trick that had snuck by undocumented. pic.twitter.com/Hb5QUJY3ix
— Taylor Otwell ????♂️ (@taylorotwell) February 11, 2018
Read more [twitter.com]
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/
PHP 7.1 will have a neat function to create closures from callables. Joseph Silber explains the function and offers some good examples on his blog.
With PHP 5.5 going EOL earlier this week and the PHP 7.1 beta expected later this month, now sounds like a good time to look into a neat little feature coming in 7.1: easily converting any callable into a proper Closure using the new Closure::fromCallable() method.
- A short refresher on closures in PHP
- When you need to convert a callable to a Closure
- Introducing Closure::fromCallable()
- Creating a closure that wraps a private method
https://josephsilber.com/posts/2016/07/13/closure-from-callable-in-php-7-1