Upgrade all our websites to PHP 8.1 and WordPress 6.2

The new version of PHP 8.1 is coming on strong and brings quite important new features, and in Close-technology we have joined all of them.

For this reason, we have compiled a list of the most interesting ones and how they affect websites. Keep reading!

Contents

Advantages of upgrading from PHP 7 to PHP 8.1

Next we will talk about the main functionalities and features of this PHP version and the advantages it brings.

The simplification of the code and adapt to new developments on the Internet are the two main advantages of this version and in which we focus on web development to get the best performance on the web.

All this is due to WordPress 6.2, a new update.

This new update of PHP 8.1, is possible because, in Close-Technology, we develop websites with WordPress, which gives us great advantages such as knowing at the moment when a website is down, requiring reestablish it.

WordPress 6.2 is the latest update of this CMS that has brought us a lot of new features that we have already applied in all our clients’ websites.

WordPress 6.2 and PHP 8.1 are updates that will be made to those customers who perform maintenance of your website with us.

There are many advantages offered by this new update, what are you waiting for to join the change?

For more information, do not hesitate to contact us.

Before we start, what is PHP 8.1?

PHP is a programming language that is mainly adapted to web development, and on which WordPress is based. Its latest update, 8.1, contains a lot of new features, optimizations and performance improvements.

Some of the most important new features are:

  • Enumerations
  • First-class Callable Syntax
  • The single-reading classes, which allows to have an immutable class, that is to say, that does not allow the modification of this after its creation.
  • New expression in constructor
  • Pure intersection types
  • Fibers
  • Never return type
  • Final class constants
  • Explicit octal numeric notation
  • Array destructuring support
  • Performance improvements
  • New classes, interfaces and functions
Php 8.1

1. Enumerations

An enum is a data type used to define possible values, such as “true” or “false”.

In general, enums are a useful way to make our code cleaner, reduce errors and make it easier to read.

With the PHP 8.1 update, this is no longer the case:

{ class Status
{
    const DRAFT = 'draft';
    const PUBLISHED = 'published';
    const ARCHIVED = 'archived';
}
function acceptStatus(string $status) {...}

To this:

{ enum Status.
{
    case Draft;
    case Published;
    case Archived;
}
function acceptStatus(Status $status) {...}

2. Fibers

This PHP 8.1 update, allows to manage the possible interruptions in the flow of your code, this means that it controls the versatility of the page, it allows that if several people work simultaneously in the programming of a page, these can carry out their work without being lost or damaged, creating a multiwork system.

With the upgrade, it goes from this:

$httpClient->request('https://example.com/')
        ->then(function (Response $response) {
            return $response->getBody()->buffer();
        })
        ->then(function (string $responseBody) {
            print json_decode($responseBody)['code'];
        });

To this:

$response = $httpClient->request('https://example.com/');
print json_decode($response->getBody()->buffer())['code'];

3. Never return type

The Never return type indicates that a function is terminated and does not return any other value, for example, if in the function “pen” you enter “blue”, “red”, and “black”, none of the three can be found in any function other than “pen”.

With the PHP 8.1 update, this is no longer the case:

function redirect(string $uri) {
    header('Location: ' . $uri);
    exit();
}
 
function redirectToLoginPage() {
    redirect('/login');
    echo 'Hello'; // <- dead code
}

To this:

function redirect(string $uri): never {
    header('Location: ' . $uri);
    exit();
}
 
function redirectToLoginPage(): never {
    redirect('/login');
    echo 'Hello'; // <- dead code detected by static analysis 
}
Php 8 1 Close

4. New expression in constructor

This feature of PHP 8.1, allows using objects as default parameter values, static variables and global constants, as well as in attribute arguments. This means that it helps web management, making it more organized.

With the update, it goes beyond this:

{ class Service 
{
    private Logger $logger;
 
    public function __construct(
        ?Logger $logger = null,
    ) {
        $this->logger = $logger ?? new NullLogger();
    }
}

To this:

class Service 
{
    private Logger $logger;
    
    public function __construct(
        Logger $logger = new NullLogger(),
    ) {
        $this->logger = $logger;
    }
}

5. Read-only properties

With this new update, you cannot edit the content once the read-only mode is started, i.e. the content is kept intact until the read-only mode is finished.

With the PHP 8.1 update, this is no longer the case:

{ class BlogData
{
    private Status $status;
   
    public function __construct(Status $status) 
    {
        $this->status = $status;
    }
    
    public function getStatus(): Status 
    {
        return $this->status;    
    }
}

To this:

class Demo {
 
    public string $Mutable;
 
    public readonly string $Immutable;
 
    public function __construct(
        string $Mutable,
        string $Immutable) {
 
        $this -> Mutable = $Mutable;
        $this -> Immutable = $Immutable;
    }
 
}

6. First-class Callable Syntax

It serves to replace existing encodings that use strings and arrays. This means that it simplifies the programmers’ language to make it much simpler.

With the update, it goes from this:

$foo = [$this, 'foo'];

$fn = Closure::fromCallable('strlen');

To this:

$foo = $this->foo(...);

$fn = strlen(...);
Php 8.1 Actualizacion

Leave a Comment

Item added to cart.
0 items - 0,00