Author - StudySection Post Views - 60 views
php

What is new in PHP8?

PHP8 release date is 26 November 2020. It is a new version with some breaking changes, new features, and improved performance. Due to breaking changes users need to make some changes in their code to execute it on PHP8.
As well some new features introduced in PHP8 are: JIT compiler, Match expression, Union type, Named argument, Constructor property promotion, Null safe operator, and more.

JIT compiler

It is Just in Time Compiler which translates the intermediate code into X86 machine code. Even though opcode is in low-level presentation still it needs to compile in X86 machine code. JIT uses the dynamic assembler for code generation engines rather than any additional intermediate representation to generate directly native code from PHP code.
Part of opcache:

  1. opcache.jit=on
  2. opcache.jit_buffer_size=128M

Benefit: It provides better performance for math-heavy code, “typical” PHP web applications and it also provides fast speed as compared to c.

Match expression

It works as a switch function but with different features.
Strictly data type is checked in the match function.
In switch after matching the case, multiple line code can be written but in the match() only one line code can be written.
If the switch function does not give default, then it will execute net but in match, it will generate an error.
Length of the match() is less as compared to switch().

Example:

<?php
class newphp8
{
public function mathexp()
{
$expcode= 100;
switch($expcode)
{
case 100:
return "successful";
break;
case 200:
return "unsuccessful";
break;
case 300:
return "error found";
break;
default:
return "undefined status";
}
return match($expcode)
{
100 => "successful";
200 => "unsuccessful";
300 => "error found";
default => "undefined status";
}
}
}
?>

Union type

More than one data type can be declared with the use of pipes to divide them in php8.
Example:
<?php
class calculation
{
public function add(int|string $a, int|string $b): int|string
{
if(is_integer($a) && is_integer($b))
return $a+$b;
if(is_string($a) && is_string($b))
return $a.$b;
}
}

Named argument

Till now a user passes the argument in a method or function that only passes the values that don’t enter the name. But in the named argument user can enter the name however user no need to remember the sequence.
Example:

class Person {
protected string $name;
protected int $age;
protected int $phone;
public function setPerson(string $name, int $age, int $phone)
{
$this->name = $name;
$this->age = $age;
$this->phone = $phone;
}
}
$person = new Person();
$person->setPerson(name: 'arrow',age: 25, phone: 36485845);
$person->setPerson(name: age: 25, phone: 36485845, name: 'arrow');

Constructor property promotion

In PHP8 Constructor property promotion are a new feature that allows declaring of the class properly and constructor assignment from its constructor.

Example:
class Person {

public function __construct(protected string $name, protected int $age, protected int $phone)
{
}
}
$person = new Person(name: 'arrow', age: 25, phone: 36485845);

Null safe operator

Using this feature user can process multiple checks in a single line. if any null or error occurs on the left side then the execution of the right side program stopped at that point.
Example:

user;
if($user!= null)
$profile= $user->profile;
if($profile!=null)
$email= $profile->email;
return email;
$email= $session?->$user?->$profile?->email;

If any of these(session, user, profile, email) is not available then it will return null and does not throw the exception.

If you have knowledge of financial accounting and you are in search of a good job, financial accounting certification can help you reach your desired goals. StudySection provides Financial accounting certification for beginner level as well as expert level people in the commerce stream. You can appear in the certification exam for free to get certified.

Leave a Reply

Your email address will not be published. Required fields are marked *