PHP 8 New Features

Choirul Anam
4 min readApr 12, 2022

Learning from UDEMY

Photo by Mohammad Rahmani on Unsplash

Assalamualaikum warrahmatullahi wabarakatuh.

String to number comparison
one of the confusing things in PHP is when we compare number and string, for example we compare 0 == “choirul”, then the result is true
why is it true? because PHP will do type jugling and change “choirul” to 0, so the result is true. In PHP 8, the String to Number comparison has been changed to avoid confusion.

Consistent type error

when we create a function and when we send an argument with the wrong data type it will result in a TypeError, unfortunately in PHP many built-in functions do not return a TypeError, instead giving a warning, to be consistent now in PHP 8 many built-in functions will error TypeError if we sent wrong data type.

<?php

strlen([]);

For more details, visit the php wiki site https://wiki.php.net/rfc/consistent_type_errors.

Just In Time Compilation

the just in time compilation feature is a feature which will speed up the execution of the PHP programs that we create

To speed things up in PHP, there is a feature called OPcache which is used to improve PHP performance, by storing the compiled PHP code in memory. Thus PHP no longer needs to re-read the PHP program code every time the program is run. PHP will read directly from the OPcache that has been stored in memory. The OPcache feature must be activated before we can use it.

after turning on OPcache then Just Intime Compilation will be executed

Just Intime Compilation
Opcache will make our program code avoid having to tokenize parsing and compile again continuously for each request, JIT will make our compilation results not need to be translated by the PHP virtual machine but directly run by the machine, JIT in PHP uses a C programming language library called DynaASM , therefore being able to translate the compiled opcodes into machine instructions.

Validation for Function Overriding
in PHP 8 there is validation when implementing abstract function in class from trait. in PHP 7 when we change the parameters and return values, it is not a problem, but in PHP 8 if we change the implementation from the abstract function, an error will automatically occur.

Private function overriding

in PHP 7 when we create a function but it turns out that in the parent there is a function with the same name even though it is private it is considered overriding, even though it is clear that private functions cannot be accessed by their descendants, in PHP 8 now private functions have nothing to do with children class, so that we are free to create a function with the same name even though in the parent there is a private function with the same name.

Mixed Type v2
in PHP 8 the mixed data type was updated, because in PHP 8 there was already a Union Type so now the mixed data type stands for data type array|bool|callable|int|float|null|object|resource|string.

Mixed type

thats for today. Thanks

Wassalamualaikum warrahmatullahi wabarakatuh.

--

--