PHP 8: All The Info On The New Update

PHP is one of the easiest programming languages to learn. With a good tutorial and the right motivation, you can write your first scripts in a matter of hours and execute commands. PHP 7 has already made it possible to optimize its speed, which has led to a better ranking. Now the open source language has received a major new update with version 8.0 .

On November 26, 2020, the latest version of the popular scripting language, PHP 8.0.0, was released. This corresponds to the normal three-year cycle of PHP training. Its predecessor PHP 7.4 was released a year earlier, just after support for PHP 7.1 was discontinued. Support for PSP 7.2 will also end at the end of 2020.

Many websites written with PHP are still using an older version. This is still possible in principle, but not recommended. There are some advantages to keeping your site code up to date and upgrading to newer versions of PHP. The new functions offer more possibilities, performance can be significantly increased and security vulnerabilities are managed.

Are there any complications with the old code?

As PHP 8 is a major new version, it is to be expected that the old code will no longer be compatible. However, most of the changes that could cause complications were already deprecated in versions 7.2 , 7.3 and 7.4 .

Some of the latest changes include:

The  real  type
Magic quotes legacy
array_key_exists ()  with objects
FILTER_SANITIZE_MAGIC_QUOTES  filter
Reflection  export ()  methods
mb_strrpos ()  with encoding as 3rd argument
implode ()  parameter order mix
Unbinding  $ this  from non-static closures
hebrevc ()  function
convert_cyr_string ()  function
money_format ()  function
ezmlm_hash ()  function
restore_include_path ()  function
allow_url_include  ini directive

If you've always kept your code up to date, you'll have virtually no problems, even if retroactive changes are inconsistent . A full list of changes in this Major Release is available on the PHP project page .

New features in PHP 8

The new version of PHP comes with new features that will provide web developers with many more possibilities. Here is a summary of the main improvements made over previous versions.

JIT compiler

The biggest innovation is the JIT compiler, which significantly improves performance. PHP is not compiled, but interpreted line by line. The JIT compiler (JIT = Just in Time) compiles parts of the code at runtime and therefore acts as a cached version of the code.

This new feature of PHP 8 has already been tested quite impressively by Pedro Escudero . The latter used a simple script to compare versions 5.3, 7.4 and 8 (with and without JIT). So he ran the script 100 times in each of the versions and then calculated the average time it took to run.

While switching from version 7.4 to version 8 without JIT does not make much difference, the difference compared to version 8 with JIT is significant. The JIT compiler provides a performance improvement of over 45% .

Union type

Union types have already appeared in other languages like C / C ++, TypeScript or Haskell. Here two or more types can form a union and any type mentioned can be used. In the code itself, we could for example have:

public function foo(Foo|Bar $input): int|float;

However, there is a restriction, because void cannot be part of a union type because it does not produce a return value. In addition, null unions can be declared with the statements | null or ? , as in this example:

public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;

Static return type

Static is a special class name; in the new version it becomes a valid Return type in addition to self & parent .

WeakMap

WeakRefs has already been added to PHP 7.4. PHP 8 now ships with WeakMaps the extension of this function. WeakMaps and WeakRefs can be used to delete objects when only the cache refers to the feature classes of the objects. This saves resources when handling objects . An example from the documentation:

class FooBar {
    private WeakMap $cache;
    public function getSomethingWithCaching(object $obj) {
        return $this->cache[$obj] ??= $this->
computeSomethingExpensive($obj);
    }
    // ...
}

:: class applied to objects

In previous versions, you had to use get_class () to assign a class to objects. Now you can apply :: class to objects, which helps reduce the size of the source code.

Stringable interface

The Stringable interface is automatically added to classes that implement the __ toString () method . Previously, this step had to be done manually. In the code itself, this is what it looks like:

class Foo
{
    public function __toString(): string
    {
        return ‘foo’;
    }
}
function bar(Stringable $stringable) { /* … */ }
bar(new Foo());
bar(‘abc’);

Fdiv () function

With the new fdiv () function, division by 0 is allowed. You then get INF , -INF or NAN as the return value.

Type annotations

PHP 8 includes correct type annotations for all built-in functions and methods.

Type errors

Previously, only user-defined functions raised TypeErrors , internal functions emitted warning and null . With PHP 8, the majority of built-in functions also return TypeErrors .

Comments

Popular posts from this blog

Python on the verge of overtaking Java in the hearts of developers

Top 8 Programming Languages For Hacking 2022

Python Project Means That It Has Built Up A Massive Backlog Of Issues