# PHP is freaking turning into JS

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

```php
$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.

![pepe_yes Discord & Slack Emoji](https://cdn3.emoji.gg/emojis/7572-pepe-yes.png align="left")

### First-class functions

Basically, you can use the function as a variable. To put a function to a variable, hit this:

```php
function transformUser(User $user): array;

$transferUserFunc = transformUser(...); // yes

// pass it to a Laravel Collection
$users->map($transferUserFunc);

// alternative
$users->map(transformUser(...));
```

![Pepe_Hype Discord & Slack Emoji](https://cdn3.emoji.gg/emojis/9519_Pepe_Hype.gif align="center")

### Falsy fallback

From JS world, you would have something like

```typescript
const value = user.firstName || user.email || null;
```

If firstName is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), it will fall to email. If email is falsy then it will fall to `null`

PHP has that one too (not `||`)

```php
$value = $user->firstName ?: $user->email ?: null;
```

![pepeCool Discord & Slack Emoji](https://cdn3.emoji.gg/emojis/3777-pepecool.png align="left")

### Null-safe operator

Aka optional chaining from JS. Super useful operator to do deep-level access.

```php
$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();
```

![happy_pepe Discord & Slack Emoji](https://cdn3.emoji.gg/emojis/1636-happy-pepe.png align="left")

### 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:

```typescript
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:

```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
);
```

![pepeSanta Discord & Slack Emoji](https://cdn3.emoji.gg/emojis/1654-pepesanta.png align="left")

### Union Types

Not JS but TS has this one, eg:

```typescript
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.

```php
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
);
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1671521613993/2qLlhs5T4.gif align="center")

## Ending

Well, that's almost it. Thanks for watching.

Coding with joy and fun, indeed!
