Downgrading Blade Component For PHP 7.4

Recount the intriguing task of reworking a PHP 8 Blade component to be compatible with PHP 7.4. This friendly narrative provides insights that will resonate with developers navigating similar upgrade paths.

Kode

Not too long ago, I found myself facing a curious challenge. I was working with a Blade component that was all decked out in PHP 8 finery. It was sleek, it was modern, and it was… well, incompatible with the project’s server that was still cozying up with PHP 7.4.

So, what’s a developer to do? Roll up their sleeves and get to work, that’s what! And I want to take you along on the step-by-step of how I took a piece of PHP 8 code and gave it a PHP 7.4 makeover.

The mission was clear: to transform a Blade component using PHP 8’s match expression and the shiny new nullsafe operator into something just as functional, but fluent in the dialect of PHP 7.4.

The original PHP 8 code was something like this:

@php
$baseClasses = 'justify-center items-center text-base font-medium rounded-lg';
$styleClasses = match($attributes['style'] ?? '') {
    'outline' => 'border border-white hover:bg-gray-100 focus:ring-4 focus:ring-gray-400 hover:text-gray-900',
    'shiny' => 'bg-gradient-to-r from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 focus:ring-4 focus:ring-blue-300',
    'flat' => 'bg-gray-200 text-gray-800 hover:bg-gray-300 focus:ring-4 focus:ring-gray-500',
    default => 'text-indigo-600 hover:underline',
};
$classes = $baseClasses . ' ' . $styleClasses;
@endphp

<a {{ $attributes->merge(['class' => $classes]) }}>
    {{ $slot }}
</a>

It was a thing of beauty, but alas, PHP 7.4 would have none of it. So, with a nostalgic sigh, I bid farewell to the match expression and greeted an old friend: the switch statement. Here’s what I did:

@php
$baseClasses = 'justify-center items-center text-base font-medium rounded-lg';
$styleClasses = '';
$style = $attributes['style'] ?? '';

switch ($style) {
    case 'outline':
        $styleClasses = 'border border-white hover:bg-gray-100 focus:ring-4 focus:ring-gray-400 hover:text-gray-900';
        break;
    case 'shiny':
        $styleClasses = 'bg-gradient-to-r from-blue-500 to-purple-600 hover:from-blue-600 hover:to-purple-700 focus:ring-4 focus:ring-blue-300';
        break;
    case 'flat':
        $styleClasses = 'bg-gray-200 text-gray-800 hover:bg-gray-300 focus:ring-4 focus:ring-gray-500';
        break;
    default:
        $styleClasses = 'text-indigo-600 hover:underline';
        break;
}

$classes = $baseClasses . ' ' . $styleClasses;
@endphp

<a class="{{ $classes }}" {!! $attributes->merge([]) !!}>
    {{ $slot }}
</a>

I admit, it felt a bit like trading in a sports car for a trusty old bicycle. But you know what? That bicycle got me where I needed to go. By replacing the match with a switch and using good old concatenation, I crafted a Blade component that was just as functional and stylish as before, while playing nice with PHP 7.4.

So, what’s the moral of the story? Always know your environment and be ready to adapt. Embrace the new, but never forget the old, because sometimes, they’re what save the day.