Yeahh boi it is true.
Let's check out some of the new features (PHP8.0+)
Recommendation: upgrade to PHP8.1, latest always better 😉
Spread
$fromAnIndexArray = [1,2,3,4];
$fromAnAssocArray = [
'seth' => 'phat',
];
$array = [
...$fromAnIndexArray,
...$fromAnAssocArray, // PHP 8.1+
];
As same as JS, this would create a freshly new array for you based on the attributes of the old array.
First-class functions
Basically, you can use the function as a variable. To put a function to a variable, hit this:
function transformUser(User $user): array;
$transferUserFunc = transformUser(...); // yes
// pass it to a Laravel Collection
$users->map($transferUserFunc);
// alternative
$users->map(transformUser(...));
Falsy fallback
From JS world, you would have something like
const value = user.firstName || user.email || null;
If firstName is falsy, it will fall to email. If email is falsy then it will fall to null
PHP has that one too (not ||
)
$value = $user->firstName ?: $user->email ?: null;
Null-safe operator
Aka optional chaining from JS. Super useful operator to do deep-level access.
$userCompany = $user->company?->name; // `null` will be assigned if company is null
$userCompanyCityName = $user->company?->country?->city?->name;
// chaing methods too
$userLevel = $user->getCompany()?->getCountry();
Named parameters
It is not exactly the same way for both, but both are having 1 purpose: to see & set the exact params when invoking a method.
For methods with multiple params, this will highly increase the readability.
JS/TS would have something like this:
type RegisterNewAccountProps = {
email: string;
password: string;
fullName: string;
age?: number;
}
function registerNewAccount({
email,
password,
fullName,
age, // can be undefined
}: RegisterNewAccountProps): User;
// invoking it
registerNewAccount({
email: 'phat@sethphat.com',
password: 'lol',
fullName: 'Seth Phat',
});
PHP:
function registerNewAccount(
string $email,
string $password,
string $fullName,
?int $age = null
): User;
// invoking it
registerNewAccount(
email: $request->input('email'),
password: $request->input('password'),
fullName: $request->input('fullName')
// you dont have to pass the null for the age
);
Union Types
Not JS but TS has this one, eg:
type RequiredFields = {
email: string;
password: string;
}
type AdditionalFields = {
firstName: string;
lastName: string;
age: int;
}
// merge 2 types, available for PHP 8.2+
type RequiredAndAdditionalFields = RequiredFields & AdditionalFields;
type CreateUserRequest = RequiredFields
| RequiredAndAdditionalFields
| ...;
PHP 8.0 supports Union Types and PHP 8.2 supports disjoint types. Differences: PHP needs to declare it in the methods' parameters.
public function createUser(
UserRequiredFields | UserRequiredAndAdditionalFields $fields
): User;
function delete(User | Business | Country $model): bool
{
// handles
return $model->delete();
}
// php 8.2+
public createUser(
UserRequiredFields | (UserRequiredFields & AdditionalFields) $fields
);
Ending
Well, that's almost it. Thanks for watching.
Coding with joy and fun, indeed!