symfony/polyfill-php84
Symfony Polyfill for PHP 8.4 features, enabling newer core functions and APIs on older runtimes. Includes array_find/any/all, bcdivmod, Deprecated attribute, fpow, grapheme_str_split, mb_* trim/ucfirst/lcfirst, PDO subclasses, and ReflectionConstant.
Installation:
Add the package to your composer.json:
composer require symfony/polyfill-php84
Laravel’s autoloader will handle the rest—no manual require needed.
First Use Case: Replace legacy array operations with PHP 8.4+ functions. Example:
// Before (Laravel 9.x/10.x on PHP ≤8.3)
$admin = collect($users)->first(fn($user) => $user['role'] === 'admin');
// After (using polyfill)
$admin = array_find($users, fn($user) => $user['role'] === 'admin');
Verify: Check php -r "echo PHP_VERSION;" to confirm your environment still reports ≤8.3.
Where to Look First:
array_find, array_find_key (replace collect()->first()).mb_trim, mb_ucfirst (fixes for null inputs in Laravel validation).#[Deprecated] in custom classes to align with Laravel’s deprecation practices.Array Operations:
collect()->first():
// Laravel Collection
$user = User::where('role', 'admin')->first();
// Polyfill alternative
$user = array_find(User::all()->toArray(), fn($u) => $u['role'] === 'admin');
array_all:
$allActive = array_all($users, fn($u) => $u['active']);
Multibyte String Handling:
$cleanInput = mb_trim($request->input('name')); // Handles emojis, CJK
$title = mb_ucfirst($request->input('title')); // Correct for non-ASCII
Deprecation Management:
#[Deprecated('Use App\Services\V2\LegacyService instead')]
class LegacyService { ... }
Illuminate\Support\Facades\DeprecatesFunctions for unified warnings.Math/Precision:
$result = bcdivmod('10.5', '3', 4); // '3.5000' (PHP 8.4+)
$power = fpow(2, -3); // 0.125 (handles negative exponents)
PDO and cURL:
$pdo->setAttribute(PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, true);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3);
Gradual Migration:
array_find to a new admin panel before upgrading the entire app.Localization Pipelines:
Str::of()->trim() with mb_trim() in multilingual validation rules:
'name' => ['required', fn($attr, $value) => mb_strlen(mb_trim($value)) > 2],
Testing Strategy:
array_find on empty arrays).mb_trim in form requests with Unicode input.array_find vs. collect()->first() in high-load routes.CI/CD Integration:
- name: Check PHP Version
run: |
if [ $(php -r 'echo PHP_VERSION;') != "8.4.0" ]; then
composer require symfony/polyfill-php84
fi
Laravel Collections:
Polyfills complement (not replace) Laravel’s collections. Use array_find for simple arrays, but prefer collect()->where()->first() for complex queries.
Service Providers: Register polyfill-aware bindings:
public function register()
{
$this->app->bind('array_finder', fn() => new class {
public function find(array $items, callable $callback) {
return array_find($items, $callback);
}
});
}
Blade Templates:
Use @php directives for polyfill-heavy logic:
@php
$highlighted = array_find($posts, fn($p) => $p['featured']);
@endphp
Database Seeds:
Leverage array_all for validation:
$validUsers = array_all($users, fn($u) => filled($u['email']));
PCRE Version Mismatch:
grapheme_str_split fails on PCRE <10.44 (common in shared hosting).if (version_compare(PCRE_VERSION, '10.44') < 0) {
throw new RuntimeException('Upgrade PCRE for grapheme_str_split support.');
}
preg_split('/\X/u', $string) as a fallback.null Handling in mb_* Functions:
mb_trim(null) throws TypeError in PHP ≤8.3 (fixed in v1.38.0+).$clean = mb_trim($value ?? '');
Array Function Edge Cases:
array_find may return false (not null) for empty arrays.$result = array_find($items, $callback) ?: null;
PDO Driver Subclasses:
PDO::getAvailableDrivers() and PDO::getAttribute(PDO::ATTR_DRIVER_NAME).Deprecated Attribute:
#[Deprecated] requires PHP 8.0+ (polyfill works on PHP 7.2+ but may not trigger warnings).DeprecatesFunctions:
use Illuminate\Support\Facades\DeprecatesFunctions;
DeprecatesFunctions::add('App\LegacyClass', '1.0', 'Use App\NewClass instead');
Performance Overhead:
array_find/array_all in microbenchmarks.if (PHP_VERSION_ID >= 80400) {
return array_find($items, $callback);
}
Verify Polyfill Loading:
composer show symfony/polyfill-php84
Ensure it’s listed under require.
Check for Overrides:
array_find behaves unexpectedly, search for custom extensions or userland overrides:
grep -r "array_find" app/
PCRE Debugging:
bootstrap/app.php:
\Log::debug('PCRE Version:', PCRE_VERSION);
Deprecation Warnings:
#[Deprecated] triggers warnings by checking error_reporting(E_ALL).Autoloading:
composer.json autoloads polyfills automatically. Avoid manual require statements.Environment-Specific Loading:
bootstrap/app.php:
if (PHP_VERSION_ID < 80400) {
require __DIR__.'/../vendor/symfony/polyfill-php84/bootstrap.php';
}
Composer Scripts:
How can I help you explore Laravel packages today?