Posts tagged with syntax

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.

What's new in PHP 7.4

stitcher.io

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]

Typed properties in PHP

stitcher.io

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 in PHP

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]

Arrow functions are (probably) coming to PHP 7.4

wiki.php.net

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]

New in PHP 7.4

stitcher.io

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]

Calling an invokable in an instance variable original

by Freek Van der Herten – 2 minute read

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…

Read more

Array destructuring in PHP

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!

Read more

What's new and changing in PHP 7.3

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!

Read more

Examples of everything new in ECMAScript 2016, 2017, and 2018

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

Read more

The List Function & Practical Uses of Array Destructuring in PHP

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.

https://sebastiandedeyne.com/posts/2017/the-list-function-and-practical-uses-of-array-destructuring-in-php

Read more

Familiarity Bias is Holding You Back: It’s Time to Embrace Arrow Functions

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.

https://medium.com/javascript-scene/familiarity-bias-is-holding-you-back-its-time-to-embrace-arrow-functions-3d37e1a9bb75

Let's hope we'll soon have array functions in PHP too.

Read more