Robson Ten贸rio
Posted on October 19, 2023
馃憠 Go to Summary
There is no wrong or right way, there is our way.
-- Someone
.
.
.
Class attributes types
WTF!?
class Customer
{
public $points;
public $available;
}
Nice!
class Customer
{
public int $points;
public bool $available;
}
.
.
.
Constructor property promotion
WTF!?
class Customer
{
public string $name;
public string $email;
public Date $birth_date;
public function __construct(
string $name,
string $email,
Date $birth_date
) {
$this->name = $name;
$this->email = $email;
$this->birth_date = $birth_date;
}
}
Nice!
class Customer
{
public function __construct(
public string $name,
public string $email,
public Date $birth_date,
) { }
}
.
.
.
Methods types
WTF!?
public function calculate($points, $available)
{
...
return $score;
}
Nice!
public function calculate(int $points, bool $available): float
{
...
return $score;
}
.
.
.
Fluent sintaxe
WTF!?
$mary = User::first();
$payment = new Payment(12.98, '2023-01-09', $mary, 4.76);
$payment->process();
Nice!
$mary = User::first();
(new Payment)
->for($mary)
->amount(12.98)
->discount(4.78)
->due('2023-01-09')
->process();
.
.
.
Encapsulate contexts
WTF!?
public function makeAvatar(string $gender, int $age, string $hairColor)
{
...
return 'https://...';
}
$user = User::first();
makeAvatar($user->gender, $user->age, $user->hairColor);
Nice!
public function makeAvatarFor(User $user): string
{
...
return 'https://...';
}
$user = User::first();
makeAvatarFor($user);
馃挅 馃挭 馃檯 馃毄
Robson Ten贸rio
Posted on October 19, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.