Paulund

PHP 7.4 New Features

New Features

PHP 7.4 comes with a few new features that can help improve performance and make for cleaner code. Here's some of the new features that come with PHP 7.4.

Class Properties

Class properties can now support type declarations.

<?php
class User {
    public int $id;
    public string $name;
    public float $x, $y;
    public iterable $iterables;
    protected ClassName $classType;
	
    // desclaring variable with nullable value
    public ?string $void = null;
}

This means that $user->id can only have integer values and $user->name can only have string values.

Arrow Functions

If you use JavaScript you'll be familiar with arrow functions, PHP now has the ability to shorthand functions by using the arrow syntax.

$factor = 10;
$nums = array_map(fn($number) => $number * $factor, [1, 2, 3, 4]);

// Instead of
$nums = array_map(function ($number) use ($factor) {
    return $number * $factor;
}, [1, 2, 3, 4]);

Array Spread Operators

Spread operators can not be used to unpack arrays.

<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];

Opcache Preload

With PHP 7.4, support for preloading was added, a feature that could improve the performance of your code significantly.

Using preload you can set PHP to load certain PHP files into memory on startup. Doing so reduces the amount of files PHP has to compile when receiving an HTTP request which improves performance.

You provide a preload script and link to it in your php.ini file using opcache.preload.

Every PHP file you want to be preloaded should be passed to opcache_compile_file() or be required once, from within the preload script.

Here is a githubb package that can wrap the preloading in a class to easily create the preloader information.

Preloader

Upgrade To 7.4 Using Rector

Rector is a fantastic tool to convert code written for an older version of PHP to use all the shiny new features of newer PHP versions.

Rector can:

  • Rename classes, methods, properties, namespaces or constants
  • Complete parameter, var or return type declarations based on static analysis of your code
  • Upgrade your code from PHP 5.3 to PHP 7.4
  • Migrate your project from Nette to Symfony
  • Complete PHP 7.4 property type declarations
  • Refactor Laravel facades to dependency injection

Rector is a composer package to install in your project or it can also be used through docker.

docker run -v $(pwd):/project rector/rector:latest process /project/app --config="/project/rector.yaml"  --autoload-file /project/vendor/autoload.php

Rector

Upgrading Docker To 7.4

Now that this blog is running on docker containers upgrading to PHP 7.4 is very simple I just need to change the PHP version of the base PHP image used in the containers

- FROM php:7.3-fpm-alpine
+ FROM php:7.4-fpm-alpine

UpChecker

Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring