naylinhtunit / PHP8-course

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PHP 8 Course

  • The Nullsafe Operator => ?->
class User {
    public function profile()
    {
        return new Profile;
    }
}

class Profile {
    public function employment()
    {
        return "Kalin";
    }
}

$user = new User();
echo $user->profile()?->employment();

Nullsafe-Operator-1

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

class Profile {
    public function employment()
    {
        return "Kalin";
    }
}

$user = new User();
var_dump($user->profile()?->employment());

Nullsafe-Operator-2

  • Null Coalescing Operator => ??
class User {
    public function profile()
    {
        return null;
    }
}

class Profile {
    public function employment()
    {
        return "Kalin";
    }
}

$user = new User();
echo $user->profile()?->employment() ?? 'Not provided';

Null-Coalescing-Operator

  • Match Expression => match(get_class())
class Conversation{}

$obj = new Conversation();

//switch(get_class($obj))
//{
//  case 'Conversation':
//    $type = 'started conversation';
//    break;
    
//  case 'Reply':
//    $type = 'started reply conversation';
//    break;
    
//  case 'Comment':
//    $type = 'started comment conversation';
//    break;
//}

$type = match(get_class($obj))
{
	'Conversation' => 'started conversation',
  	'Reply' => 'started reply conversation',
  	'Comment' => 'started comment conversation',
};

var_dump($type);

Match-Expression

  • Constructor Property Promotion => __constructor(protected $name)
//class User {
//	protected $name;
//  	public function __construct($name)
//    {
//      $this->name = $name;
//    }
//}

//class Plan {
//	protected $name;
//  	public function __construct($name)
//    {
//    	$this->name = $name;
//    }
//}

//class Signup {
//	protected User $user;
//  	protected Plan $plan;
//  	public function __construct($user, $plan)
//    {
//    	$this->user = $user;
//      	$this->plan = $plan;
//    }
//}

//$user = new User("Kalin");
//$plan = new Plan("PHP8");

//$signup = new Signup($user, $plan);

//var_dump($signup);


class User {
  	public function __construct(protected $name)
    {
    }
}

class Plan {
  	public function __construct(protected string $name = 'PHP')
    {
    }
}

class Signup {
  	public function __construct(protected User $user, protected Plan $plan)
    {
    }
}

$user = new User("Kalin");
$plan = new Plan();

$signup = new Signup($user, $plan);

var_dump($signup);

Constructor-Property-Promotion

  • $obj::class
class Conversation {
    //
}

$obj = new Conversation();

$type = match($obj::class)
{
    'Conversation' => 'started conversation',
    'Reply' => 'started reply',
    'Comment' => 'started comment',
};

var_dump($type);

obj-class

  • Named Parameters
class Invoice {
    private $description;
    private $total;
    private $date;
    private $paid;

    public function __construct($description, $total, $date, $paid)
    {
        $this->description = $description;
        $this->total = $total;
        $this->date = $date;
        $this->paid = $paid;
    }
}

$invoice = new Invoice(
    description: 'Customer Installation',
    total: 1000,
    date: new DateTime(),
    paid: true
);

var_dump($invoice);

Named-Parameters

About