PHP is freaking turning into JS

PHP is freaking turning into JS


3 min read

Yeahh boi it is true.

Let's check out some of the new features (PHP8.0+)

Recommendation: upgrade to PHP8.1, latest always better ๐Ÿ˜‰


$fromAnIndexArray = [1,2,3,4];
$fromAnAssocArray = [
    'seth' => 'phat',

$array = [
    ...$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

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

// alternative

Pepe_Hype Discord & Slack Emoji

Falsy fallback

From JS world, you would have something like

const value = user.firstName || || 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;

pepeCool Discord & Slack Emoji

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

happy_pepe Discord & Slack Emoji

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({
    age, // can be undefined
}: RegisterNewAccountProps): User;

// invoking it
    email: '',
    password: 'lol',
    fullName: 'Seth Phat',


function registerNewAccount(
    string $email,
    string $password,
    string $fullName,
    ?int $age = null
): User;

// invoking it
    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

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


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

Coding with joy and fun, indeed!