PHP 8 is available on Scalingo!

Good news : PHP8 is now available on Scalingo

PHP 8.0 is a major update of the PHP language. We made it available on Scalingo!

In this article we will explain how you can switch to PHP 8 on Scalingo and what are the major changes of PHP 8.

Overall PHP 8 aims to reach better performance (we will detail the cases) with the introduction of JIT (Just-in-Time compilation).

Moreover PHP 8 adds syntactic sugar to make PHP code easier to write (and read!). This will help reduce maintenance cost of your PHP code base.

Let’s dive into it!

Enabling PHP 8 for Your App on Scalingo

Here is an example to install the latest PHP version from the 8.0 branch:

{
  "require": {
    "php": "8.0"
  }
}

This will install the version 8.0.3 at the time of writing.

By default on Scalingo, the latest 7.4 version of PHP will be installed, if you need to install a precise version it should be mentioned in your composer.json file.

The reason for having 7.4 as default is because a few PHP extensions are not yet compatible with PHP 8 (Lua, Data Structures, GMP).

As always, we have documented everything and you can check which native php extensions are already available for PHP 8.

While these extensions are not supported, we stick with PHP 7.4 as the default.

If you need more information about PHP hosting on Scalingo, you can read our detailed PHP documentation.

What’s new on PHP 8?

PHP JIT - Just In Time compilation

What is PHP JIT?

Here is how RFC describes JIT-Just in Time compilation :

PHP JIT is implemented as an almost independent part of OPcache. It may be enabled/disabled at PHP compile time and at run-time. When enabled, native code of PHP files is stored in an additional region of the OPcache shared memory and op_array->opcodes[].handler(s) keep pointers to the entry points of JIT-ed code. This approach doesn’t require engine modification at all.

As stated by RFC, the PHP team believes that they have reached their ability to improve PHP’s performance using other optimization strategies. Consequently using JIT is a response to improve the performance of PHP.

According to some tests here is the relative JIT contribution to PHP 8 performance :

Impact of PHP 8 on performance
Graph Php.net

Graph php.net. PHP 8 introduces two JIT compilation engines : Tracing JIT and Function JIT. Tracing JIT (in blue) can offer up to 2.9 times the performance of PHP without JIT. This graph can help you take a decision on moving to PHP 8 or not.

Named arguments

Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily.

Here is what you can do with PHP 8 :

  • Specify only required parameters, skipping optional ones.
  • Arguments are order-independent and self-documented.

Attributes

Again attributes are here to make PHP code easier to write.

Instead of PHPDoc annotations, you can now use structured metadata with PHP’s native syntax.

PHP 7
class PostsController
{
    /**
     * @Route("/api/posts/{id}", methods={"GET"})
     */
    public function get($id) { /* ... */ }
}
PHP 8
class PostsController
{
    #[Route("/api/posts/{id}", methods: ["GET"])]
    public function get($id) { /* ... */ }
}

Constructor property promotion

With PHP 8 you will have less boilerplate code to define and initialize properties.

Here is an example from php.net

PHP 7
class Point {
  public float $x;
  public float $y;
  public float $z;
  public function __construct(
    float $x = 0.0,
    float $y = 0.0,
    float $z = 0.0
  ) {
    $this->x = $x;
    $this->y = $y;
    $this->z = $z;
  }
}
PHP 8
class Point {
  public function __construct(
    public float $x = 0.0,
    public float $y = 0.0,
    public float $z = 0.0,
  ) {}
}

Union types

Again PHP 8 is simplifying Union types. Instead of PHPDoc annotations for a combination of types, you can use native union type declarations that are validated at runtime.

Again here is an example from PHP.net

PHP 7
class Number {
  /** @var int|float */
  private $number;
  /**
   * @param float|int $number
   */
  public function __construct($number) {
    $this->number = $number;
  }
}
new Number('NaN'); // Ok
PHP 8
class Number {
  public function __construct(
    private int|float $number
  ) {}
}
new Number('NaN'); // TypeError

Nullsafe operator

PHP 8 also introduces Nullsafe operator that would simplify PHP code. Instead of using null check conditions, you can now use a chain of calls with the new nullsafe operator.

PHP 7
$country =  null;
if ($session !== null) {
  $user = $session->user;
  if ($user !== null) {
    $address = $user->getAddress();
 
    if ($address !== null) {
      $country = $address->country;
    }
  }
}
PHP 8
$country = $session?->user?->getAddress()?->country;

Conclusion

PHP 8 is bringing a lot of novelties to the table and we are very happy to announce that PHP 8 is now supported on Scalingo!

There are many other features and improvement that comes with PHP 8. We invite you to read the full release announcement on php.net.

Image Ben Unsplash