
PHP 7.4.0 is now Released!
Today, the PHP development team announced the immediate availability of PHP 7.4.0 and it comes with numerous improvements and new features.
Some of the highlights of this release that we’ve covered before include:
Short Arrow Functions:
- The PHP team recently approved the Short Arrow Functions RFC proposed by Nikita Popov, Levi Morrison, and Bob Weinand.
- this as an example to give you an idea of how it can be used:
$extended = function ($c) use ($callable, $factory) {
return $callable($factory($c), $c);
};
// with arrow function:
$extended = fn($c) => $callable($factory($c), $c);
A Laravel example Look like this:
// Current
$users->map(function($user) {
return $user->first_name.' '.$user->last_name;
});// with arrow function:
$users->map(
fn($user) => $user->first_name.' '.$user->last_name
);
Short Arrow Functions is targeted for inclusion in PHP v7.4, and you can read more about this on the PHP wiki and listen to the PHP Internals Podcast where Nikita Popov joins them to discuss this change.
Spread Operator for Arrays:
- The RFC vote for spread operator support in Array expressions was overwhelmingly in favor of adding this feature to PHP 7.4.
- The spread operator support for argument unpacking first existed in PHP 5.6, and this RFC expands on the usage to arrays; both arrays and objects that support Traversable can be expanded. Here is a basic example from the RFC:
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
String keys are not supported; you can only use indexed arrays. The author of the RFC explains key support as follows:
In order to make the behavior consistent with argument unpacking , string keys are not supported. A recoverable error will be thrown once a string key is encountered.
PHP Approves Short Arrow Functions
To learn about the full details of this accepted proposal, check out the PHP: RFC:spread_operator_for_array. We want to thank CHU Zhaowei for writing this RFC (and everyone involved). We think it’s going to be an excellent addition to PHP!
Typed Properties
- The Typed Properties 2.0 RFC was accepted with a vote of 70 in favor and one no vote. A 2/3 majority is required because typed properties is a language change. The typed property change is a PHP 7.4 proposal.
With the introduction of scalar types and return types, PHP 7 greatly increased the power of PHP’s type system. However, it is currently not possible to declare types for class properties, forcing developers to use getter and setter methods instead to enforce type contracts. This requires unnecessary boilerplate, makes usage less ergonomic and hurts performance. This RFC resolves this issue by introducing support for first-class property type declarations.
The RFC provides an example where the goal is to enforce type-safety:
class User {
/** @var int $id */
private $id;
/** @var string $name */
private $name;public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}public function getId(): int {
return $this->id;
}
public function setId(int $id): void {
$this->id = $id;
}public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
}
With the new accepted RFC, that code could be functionally equivalent as follows “without sacrificing type safety”:
class User {
public int $id;
public string $name;public function __construct(int $id, string $name) {
$this->id = $id;
$this->name = $name;
}
}
Finally, here’s an example of all the valid types supported in runtime-enforced annotations for typed properties:
class Example {
// All types with the exception of "void" and "callable" are supported
public int $scalarType;
protected ClassName $classType;
private ?ClassName $nullableClassType;// Types are also legal on static properties
public static iterable $staticProp;// Types can also be used with the "var" notation
var bool $flag;// Typed properties may have default values (more below)
public string $str = "foo";
public ?string $nullableStr = null;// The type applies to all properties in one declaration
public float $x, $y;
// equivalent to:
public float $x;
public float $y;
}
On the other end of the spectrum, PHP is usable in a completely different paradigm of a fully dynamic language embracing type coercion. However, with the continuation of new type-safety features introduced in PHP 7, it’s unclear how practical it will remain to use a fully dynamic approach to writing PHP applications.
What does that mean for those who want to program without types? Only time will tell, but I predict that at least a small majority of programmers wishing the language to stay with its dynamic roots will look to other languages also.
We are developing a website in PHP language, We happy to announce that now there is also migration avail in PHP manual that includes a detailed list of new features and a few changes.