PHP 8 at a glance

PHP 8 at a glance


#NullSafe Operator

<?php

class User {
    public function profile(){
     return null;
   }
}

class Profile {
     public function jobTtile() {
       return 'Software Engineer';
    }
}

$user = new User;        


Before PHP8 we were doing an empty check but after PHP 8 it becomes easy.

//php 7.4 or before

$profile = $user->profile();

if($profile) echo $profile->jobTitle();

//php 8

echo $user->profile()?->jobTitle();        

we can do it anywhere. Like if we have a user, and the user has a profile and then gives me the job title. Then the NullSafe operator will be like

$user?->profile()?->jobTitle();        


#Match Expression


</php

class Conversation {}

$obj = new Conversation();

switch(get_class($obj)) {
     case 'Conversation' :
         $type = 'Hello';
         break;

     case 'Reply' :
         $type = 'Hi';
         break;

     case 'Comment' :
         $type = 'Looking WOW'
         break;
}

echo $type;        

But in PHP 8 it can easily be handled by match

</php

class Conversation {}

$obj = new Conversation();

match(get_class($obj)) {
  'Conversation' => 'Hello',
  'Reply' => 'Hi',
  'Comment' => 'Looking WOW'
}

echo $type;        



#Constructor Property Promotion

It’s typical for constructor arguments to be allocated to a property in the constructor but never used. PHP 8 added constructor promotion as a convenient shorthand for such a scenario.


class Order
{
    public int $id;

    public string $description;

    public function __construct(string $id, string $description) {
        $this->id = $id;
        $this->description = $description;
    }
}        

In PHP 8 shorthand use

class Order
{
    public function __construct(public int $id, public string $description)
    {
    }
}        

#Object: Class

In PHP 7.4 you cannot use $obj::class. This throws an error. but in PHP 8 you can directly call the class

</php

class Conversation {}

$obj = new Conversation();

switch(get_class($obj::class)) {
     case 'Conversation' :
         $type = 'Hello';
         break;

     case 'Reply' :
         $type = 'Hi';
         break;

     case 'Comment' :
         $type = 'Looking WOW'
         break;
}



// or

match(get_class($obj::class)) {
  'Conversation' => 'Hello',
  'Reply' => 'Hi',
  'Comment' => 'Looking WOW'
}

echo $type;        


#NamedParameter


It could create confusion after someone again does something here. Then he needs to go to the order class and what is true need to know. The below code is PHP7.4.

<?php

class Order
{
    public function __construct(private $item, private $descrption, private $price, private $isPaid)
    {
    }
    
}
$order = new Order(1, 'Burger', 100, true);        

But PHP 8 has given us flexibility. we can add the parameter name directly. Changing the ordering in Order Class will throw an error so that you can easily find out the issue of your code. Again if you change the parameter name then you also need to change it otherwise it will throw an error. Be careful when you are changing the parameter name for refactoring or other purpose.

<?php

class Order
{
    public function __construct(private $item, private $descrption, private $price, private $isPaid)
    {
    }

}

$order = new Order(item : 1, descrption : 'Burger', price : 100, isPaid : true);        

#New String Helper

str_start_with
$id = 'INV_ADC123';

//php 7.4
$result = stripos($id, 'INV') === 0;

//php 8.0
$result = str_starts_with($id, 'INV_');

str_ends_with
$id = 'ADC123_INV';

//php 7.4
$result = stripos(strrev($id), strrev('_INV')) === 0;

//php 8.0
$result = str_ends_with($id, 'INV_');

str_contains
$url = 'https://www.shohoz.com?bus-tickets?org=dhk';
//php 7.4
$result = strpos($url, '?') !==false

//php 8.0
$result = str_contains($url, '?');        

#Union & Pseudo Types

<?php
declare(strict_type = 1)

class User {
    public function cancelOrder(bool $immediate = false) {
     var_dump($immediate);
    }

}

$user = new User;
$user->cancel(); 
$user->cancel(true);
$user->cancel('next Month');        

If we run this, this will throw an error, But php 8 has added a great feature union. we need to pass the union operator to check this. But you cannot do this in PHP 7.4. You can add mixed, callable, ittarable


<?php
declare(strict_type = 1)

class User {
    public function cancelOrder(bool|string $immediate = false) {
     var_dump($immediate);
    }

}

$user = new User;
$user->cancel(); 
$user->cancel(true);
$user->cancel('next Month');        


要查看或添加评论,请登录

社区洞察

其他会员也浏览了