php 8 new code example

Example 1: php8

// php8 is null or not
$country = $session?->user?->getAddress()?->country;

//-----------------------------
// php7 below.
$country =  null;

if ($session !== null) {
  $user = $session->user;

  if ($user !== null) {
    $address = $user->getAddress();
  
    if ($address !== null) {
      $country = $address->country;
    }
  }
}

Example 2: php8

// php8 class and construct
class Point {
  public function __construct(
    public float $x = 0.0,
    public float $y = 0.0,
    public float $z = 0.0,
  ) {}
}

Tags:

Php Example