- 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](https://camo.githubusercontent.com/f53b75feb6e00633221e5f5d9e6bce5ef06f8701d0ffc8216a659faa90ed6a30/68747470733a2f2f692e6962622e636f2f536d50346d6d772f4e756c6c736166652d4f70657261746f722d312e706e67)
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](https://camo.githubusercontent.com/b0bf6d2f1df044e149646611488ca9729f63f4f3f2bda6a0744c9819a1e0c964/68747470733a2f2f692e6962622e636f2f707a787346514d2f4e756c6c736166652d4f70657261746f722d322e706e67)
- 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](https://camo.githubusercontent.com/1974fbe4565f3b0a18eb317a405e7683d718797ff569173f99158327052e4e69/68747470733a2f2f692e6962622e636f2f514a67687636502f4e756c6c2d436f616c657363696e672d4f70657261746f722e706e67)
- 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](https://camo.githubusercontent.com/c558eec1b966de853dbda9bfa31c5a5078de9e0a26ca73063154dbbbf60a1f33/68747470733a2f2f692e6962622e636f2f66445a6e34424c2f4d617463682d45787072657373696f6e2e706e67)
- 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](https://camo.githubusercontent.com/b120312e269a5b089a06ad6e414c5f571ab7b819f078ce7aa9e014ba034dde20/68747470733a2f2f692e6962622e636f2f785348465839762f436f6e7374727563746f722d50726f70657274792d50726f6d6f74696f6e2e706e67)
class Conversation {
//
}
$obj = new Conversation();
$type = match($obj::class)
{
'Conversation' => 'started conversation',
'Reply' => 'started reply',
'Comment' => 'started comment',
};
var_dump($type);
![obj-class](https://camo.githubusercontent.com/5fa1449335df40054c84397a9d67fb4defedd9b2dad601a0ceb5c5c602356e9a/68747470733a2f2f692e6962622e636f2f586a765046734b2f6f626a2d636c6173732e706e67)
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](https://camo.githubusercontent.com/c37892a9a90556f064b7d9d488d2c160e232003ab74d2ae93b83a68f745c0411/68747470733a2f2f692e6962622e636f2f633146684472672f4e616d65642d506172616d65746572732e706e67)