You Must Know the PHP 8.0 New Features

PHP 8.0 New Features – PHP 8.0 has been released on 26 November 2020 with a major update. It comes with many new features like Union Type, Nullsafe operator, named arguments, match expression, Constructor property promotion, static return type, trailing comma & more.

Even It introduced many improvements in speed, security, and cleanness of code. Here, we will discuss only the main features that will be frequently used in PHP 8.

In this tutorial, You will know What are the new features in PHP 8 with some examples. If you are currently searching for a job in PHP then You must know about the PHP 8 new features that will be definitely asked by the interview.

php 8 new features

What are the New Features in PHP 8

Now, We will discuss the following 10 new features of PHP 8.0 that can be frequently used to build web applications.

Read Also – 

Create Admin Panel in PHP

How to Develop Autocomplete Search in PHP

 

1. Union Type Declarations

Union Type declarations allow us to declare multiple datatypes with a variable, argument, & method.

Example –

Suppose that, We have a method add() that has two parameters $a & $b. If we need to calculate addition dynamically and return the strings or integer types of the output. then we can write the code as –

<?php
class calculator{

public function add( int|string $x, int|string $y):int:string{
  if(is_integer($x) && is_integer($y)){
    return $x + $y;
  }elseif(is_string($x) && is_string($y)){
    return $x+$y;
  }
}
}

$cal = new calculator();
echo $cal->add(5, 3)  // output 8
echo $cal->add("Coding", "Status")   // output CodingStatus

?>

 

2. Nullsafe operator

Nullsafe Operator allows us to call multiple properties or methods with the ?-> . if any one of them returns null values then it will just return the null values without giving an error.

Example –

<?php
class Users{ 
  public function profile(){
    $email_address= "codingstatus@gmail.com";
  } 
}

?>

Before PHP 8.0, We write the following code

<?php

$email = null;
$users = new Users();
if($users != null)
  $profile= $users->profile();

if($profile != null)
  $email = $profile->email_address;

?>

In PHP 8.0, we can write the above code with the Nullsafe operator in a single line

$email = $users?->profile?->email_address;

 

3. Named arguments

Named arguments allow us to pass values with the name of function parameters. So, We need not to pass the values in sequence order as declared with the function.

Example –

Suppose that we have a function profile() that has three paramters like $fullName, $age, & $emailAddress

function profile(string $fullName, int $age, string $emailAddress){

   $full_Name= $fullName;
   $age = $age;
   $email_address = $emailAddress;
}

Before PHP. 8.0

We have to pass the values with a sequence order like

profile("Noor Khan", 25, "codingstatus@gmail.com")

We can’t pass the value like

profile(25, "Noor Khan", "codingstatus@gmail.com")

In PHP 8.0

We can pass the values with function parameter like

profile(fullName: "Noor Khan", age: 25, emailAddress: "codingstatus@gmail.com")

We can also pass the value without its sequence order

profile(age: 25, fullName: "Noor Khan", emailAddress : "codingstatus@gmail.com")

4. Match Expressions

The Match Expressions work similar to switch statement but It has the following more additional features features

  • Match expressions do not use the break keyword.
  • Also, Match expressions can also use the default keyword.
  • Match expressions follow the strick type conditions

Syntax –

match(condition){

  expression1 => value1,
  expression2 => value2,
  expression3 => value3,
  --------------------,
-----------------------,
  expression(n) => value(n),
 default => default value
}

Example –

In the case of Switch Statements –

<?php

$day = 5;

swicth($day){

  case 1:
  echo "Monday";
  break;
  case 2:
  echo "Teusday";
  break;
  break;
  case 3:
  echo "Wednesday";
  break;
  break;
  case 4:
  echo "Thirsday";
  break;
  break;
  case 5:
  echo "Friday";
  break;
  case 6: 
  echo "Saturday";
  break;
  case 7:
  echo "Sunday"
  break;
  default:
  echo "Day code not valid";

}
?>

In the case of Match Expressions –

$day = 5;
match($day){
1 => "Monday",
2 => "Tuesday",
3 => "Wednesday",
4 => "Thirsday",
5 => "Friday",
6 => "Saturday",
7 => "Sunday",

}

 

5. Constructor property promotion

Constructor property promotion allows us to declare class properties with a constructor. Let’s understand with the following example –

Example –

Before PHP 8.0

<?php

class Users{

 protected full_name;
 protected $age;
 protected email_address;

public function __construct(string $fullName, int $age, string $emailAddress){

  $this->full_name = $fullName;
  $this->age = $age;
  $this->email_address = $emailAddress;

}
}

?>

In PHP 8.0

<?php
class Users{

public function __construct(protected string $fullName, protected int $age, protected string $emailAddress){
  
}
}
?>

6. Mixed Type

Mixed Type is represented as mixed and it is used with a parameter or property & return type method.

Even Mixed has many types of datatype like int, string, float, object, array, bool, null, callable,& resource

The mixed type has already a null type. So, It does not allow to make nullable.

Example –

class Users {
    public mixed $fullName;
    public function profile(mixed $emailAddress): mixed {}
}

7. Trailing comma

Trailing comma comes allows us to add a comma at the last parameter of a function.

Example –

Before PHP 8.0

We can’t add a comma to the end of the last parameter

function($a, $b, $c){
}

In PHP 8.0

We can add a comma to the end of the last parameter

function($a, $b, $c,){ }

 

My Suggestion

Dear Developers, I have explained here only the required new features of PHP 8.0. I hope you have understood given each point. If want to know more, you can feel free to visit the official website URL of PHP 8.

Even you have any query regarding PHP 8.0, you can ask me through the comment box. I will definitely reply as soon as possible.

Categories PHP

Leave a Comment