Ashley Sheridan​.co.uk

PHP 7.4 Upcoming Features

Posted on

Tags:

The last few years has seen PHP undergo an incredible amount of work to improve the language. The latest couple of proposals for the 7.4 release contains a major feature that many have wanted in PHP for years: typed properties on object instances.

Typed Properties

This is the big one we've all been waiting for. As a developer who works across many languages, being able to type the properties of classes is something I've sorely missed in PHP, and it's meant that I've had to write lots of extra code for getters and setters to ensure that object instances cannot be created with invalid states. The typed properties 2.0 proposal builds upon earlier type hinting for function and method arguments, and allows typing for all supported types (with the exception of void and callable). This includes all scaler types and custom classes.

The RFC gives a great example of how the boilerplate code for a custom class can be reduced.

Original code:

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; } }

After the proposed changes:

class User { public int $id; public string $name; public function __construct(int $id, string $name) { $this->id = $id; $this->name = $name; } }

This is a great change, and will make code cleaner, more maintainable, and hopefully more free of typing errors. All good things, and it brings no breaking changes, so it should be welcome all round!

Hash Extension to Always be Available

If you're unfamiliar, the Hash Extension is used for generating all kinds of hashes using the registered algorithms. For example, to create a hash using the popular sha256 method, you would do this:

$hash = hash("sha256", "your secret data");

This would output a string like this (assuming it's not initialised with a shared key):

0e37c2ab923db3d6e3046a97786ee0d99e47a9f491f62836e91f3670b3698c85

The author of the RFC, core developer and release manager Kalle Sommer Nielsen, made the proposal that the hashing extension is so important and useful, that it needs to be afforded the same special status as the date, spl, and pcre extensions, and be always available, and impossible to disable.

Effectively, this makes the extension a guarantee for developers. If you're running PHP 7.4, then this will be available. While it's not a major change, it's welcome nonetheless, as it will mean one less detail that developers will need to concern themselves with when setting up PHP installations.

The only backwards compatibility issues are to remove the -enable-hash build configuration argument, which means a small update to builds scripts.

Conclusion

This is a very exciting release for PHP, and proves again that PHP is a strong tool with a very forward looking approach. I'm incredibly pleased that the typed properties proposal made the cut, as it brings in a strong feature that I find incredibly useful in C# and TypeScript.

Comments

Leave a comment