On using arrow functions in PHP 7.4 original
In PHP 7.4 a widely requested feature landed: arrow function. In this blogpost I'd like to show you how I like to use them.
Posts tagged with syntax
In PHP 7.4 a widely requested feature landed: arrow function. In this blogpost I'd like to show you how I like to use them.
My colleague Brent explains everything that's coming in PHP 8. Union types alone will make it a worthwile release.
Read more [stitcher.io]
Join 9,500+ smart developers
Every month I share 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.
PHP 7.4 will probably be released this week. My colleague Brent wrote a good post on all the shiney new things this release brings.
PHP 7.4, the latest version before PHP 8, brings lots of new features, syntax additions and fixes. We need to wait a little longer though: PHP 7.4 will be released on November 28, 2019. In this post you'll find a list with everything that's new and changed to help you prepare for the upgrade.
Read more [stitcher.io]
When the void typehint was introduced in PHP 7.1. There was some debate about it. Some people wondered if it is beneficial to type nothing? I was one of them. Meanwhile, I changed my opinion on it. In this short post, I'd like to give you a small example where I think void shines.
At the PHP Russia conference, Nikita Popov presented all the nice changes coming to PHP 7.4.
Brent wrote another nice post on PHP. This time on the upcoming typed hints feature of PHP 7.4.
Typed class properties were added in PHP 7.4 and provide a major improvement to PHP's type system. These changes are fully opt-in and backwards compatible. In this post we'll look at the feature in-depth.
Read more [stitcher.io]
Short closures are coming to PHP 7.4. In this blogpost, my colleague Brent exaplins what they look like and how they can be used
Short closures, also called arrow functions, are a way of writing shorter functions in PHP. This notation is useful when passing closures to functions like array_map or array_filter.
Read more [stitcher.io]
Exciting times in PHP land. Nikita Popov, Levi Morrison and Bob Weinand have officially proposed a concrete implementation for arrow functions.
Anonymous functions in PHP can be quite verbose, even when they only perform a simple operation. Partly this is due to a large amount of syntactic boilerplate, and party due to the need to manually import used variables. This makes code using simple closures hard to read and understand. This RFC proposes a more concise syntax for this pattern. ... Short closures are critically overdue, and at some point we'll have to make a compromise here, rather than shelving the topic for another few years.
Let's hope this one gets accepted!
Read more [wiki.php.net]
PHP 7.4, which will be released around December 2019, will bring a couple of nice features such as typed properties, preloading, improved type variance, ... In a new post on his blog my colleague Brent gives a nice overview of what to expect
Read more [stitcher.io]
Invokables in PHP are classes that you can use as a function. They have been around since PHP 5.3 and have many interesting use cases. Here's a quick example. class Invokable { public function __invoke() { echo 'I have been invoked'; } } You can use it like this: // outputs 'I have been…
Love being able to catch multiple exception types in one catch block thanks to PHP 7.1 pic.twitter.com/YthVWxKG0A
— Jacob Bennett (@JacobBennett) May 10, 2018
Read more [twitter.com]
Frank de Jonge, author of the great EventSauce and Flysytem packages, wrote a blopost on how to use array destructuring in PHP.
In PHP 7.1 the array type has become even more powerful! An RFC was accepted (and implemented) to provide square bracket syntax for array destructuring assignment. This change to the language made it possible to extract values from array more easily.
https://blog.frankdejonge.nl/array-destructuring-in-php/
Not mentioned in Frank's excellent post is PHP's ability to destructure arrays in foreach loops.
$members = [
[1, 'Seb'],
[2, 'Alex'],
[3, 'Brent'],
];
foreach ($members as [$id, $name]) {
// do stuff with $id and $name
}
You can even specify in which variable a value of a specific key should go:
$members = [
['id' => 1, 'name'=> 'Seb', 'twitter' => '@sebdedeyne' ],
['id' => 2, 'name'=> 'Alex', 'twitter' => '@alexvanderbist'],
['id' => 3, 'name'=> 'Brent', 'twitter' => '@brendt_gd'],
];
foreach ($members as ['twitter' => $twitterHandle, 'name' => $firstName]) {
// do stuff with $twitterHandle and $firstName
}
Very neat!
Ayesh Karunaratne made a good summary of the new stuff coming in PHP 7.3 which will be released by the end of the year.
This is a live document (until PHP 7.3 is released as generally available) on changes and new features to expect in PHP 7.3, with code examples, relevant RFCs, and the rationale behind them, in their chronological order.
https://ayesh.me/Upgrade-PHP-7.3
The trailing comma in function and method calls seems nice!
It’s hard to keep track of what’s new in JavaScript (ECMAScript). And it’s even harder to find useful code examples. So in this article, I’ll cover all 18 features that are listed in the TC39’s finished proposals that were added in ES2016, ES2017, and ES2018 (final draft) and show them with useful examples.
https://medium.freecodecamp.org/here-are-examples-of-everything-new-in-ecmascript-2016-2017-and-2018-d52fa3b5a70e
My colleague Brent published a new blogpost on how the ternary and null coalescing operators work in PHP.
You've probably used the ternary ?: and the null coalescing ?? operators in PHP. But do you really know how they work? Understanding these operators makes you use them more, resulting in a cleaner codebase.
? New in PHP 7.3: A trailing comma is now allowed in function & method calls ?? https://t.co/SeNqfbrbRW pic.twitter.com/auBOVE9FpZ
— SammyK, (@SammyK) 4 november 2017
Read more [twitter.com]
? My buddy @thatguychriswJS showed me that you can use the "plus equals" operator for a sort of "array merge assignment" - pretty sweet pic.twitter.com/UJEUlfSs7E
— Caleb Porzio (@calebporzio) September 7, 2017
Read more [twitter.com]
Vitaly Gordon explains how the little known comma operator makes it easy to add some debugging statements in short hand array functions.
What if you want to add a console.log call before x + 1 ? With comma operator, adding side-effects to functional expressions is simple:const fun = x => (console.log (x), x + 1)
https://medium.com/@xpl/javascript-did-you-know-about-the-comma-operator-ff9b511cc33
Sebastian De Deyne wrote a cool blogpost on array destructuring in PHP. Yet another reason to stay up to date with the latest and greatest PHP version.
PHP 7.1 introduced a new syntax for the list() function. I've never really seen too much list() calls in the wild, but it enables you to write some pretty neat stuff.This post is a primer of list() and it's PHP 7.1 short notation, and an overview of some use cases I've been applying them to.
I don't think that less lines of code automatically means code is more readable, but I'm a big fan of ES6' arrow functions. In this article Eric Elliott dives deep into them.
I also supect that your team would become significantly more productive if you learned to embrace and favor more of the concise syntax available in ES6. While it’s true that sometimes things are easier to understand if they’re made explicit, it’s also true that as a general rule, less code is better. If less code can accomplish the same thing and communicate more, without sacrificing any meaning, it’s objectively better.
Let's hope we'll soon have array functions in PHP too.
The future of #php (hopefully) - 2 RFCs in discussion right now: "Arrow Functions" and "Pipe Operator" pic.twitter.com/LVPiIHwGrZ
— Caleb Porzio (@calebporzio) April 19, 2017