symfony/polyfill-php73
Symfony Polyfill for PHP 7.3: provides fallback implementations of core features for older PHP versions, including array_key_first/last, hrtime, is_countable, and JsonException, ensuring consistent behavior across environments.
Installation:
composer require symfony/polyfill-php73
Add to composer.json under require:
"symfony/polyfill-php73": "^1.37.0"
First Use Case:
Replace legacy array traversal with array_key_first in a Laravel controller:
// Before (PHP <7.3)
$firstKey = array_search($value, $array) ?: reset($array);
// After (works on PHP <7.3 with polyfill)
$firstKey = array_key_first($array);
Verify Compatibility:
Check PHP version in composer.json:
"config": {
"platform": {
"php": "7.2"
}
}
symfony/polyfill in vendor/.Array Introspection:
// Eloquent: Get first key of attributes
$firstAttribute = array_key_first($model->getAttributes());
// Collections: Optimize array operations
$lastKey = array_key_last($request->all());
Error Handling:
try {
$data = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
Log::error("Invalid JSON: " . $e->getMessage());
}
Performance Metrics:
$start = hrtime(true);
// Code to benchmark
$duration = hrtime(true) - $start;
Countable Checks:
if (is_countable($items)) {
$count = count($items);
}
Phased Migration:
JsonException in API routes now, upgrade PHP later.json_decode hacks with JSON_THROW_ON_ERROR.Package Development:
composer.json but use array_key_first internally.hrtime for benchmarking.Cross-Environment Consistency:
array_key_first usage across dev/staging/prod (PHP 7.2–8.x).Laravel Collections:
Extend Illuminate\Support\Collection to use polyfilled functions:
$firstKey = $collection->keys()->first();
// Internally uses array_key_first if polyfill is loaded.
Service Providers: Autoload polyfills conditionally:
if (version_compare(PHP_VERSION, '7.3.0', '<')) {
require __DIR__.'/../../vendor/autoload_files.php';
}
Testing: Mock polyfills in PHPUnit:
$this->getMockBuilder('Symfony\Polyfill\Php73\Php73')
->disableOriginalConstructor()
->onlyMethods(['array_key_first'])
->getMock();
Function Overrides:
array_key_first or JsonException.PHP Version Mismatch:
php:7.0 minimum in composer.json.Unused Polyfills:
vendor/bin/phpstan analyse --level 5
JsonException Incompatibility:
JsonException to extend RuntimeException.JsonException inheritance:
if (!($e instanceof \JsonException)) {
// Fallback for older libraries
}
Verify Polyfill Load:
if (!function_exists('array_key_first')) {
throw new \RuntimeException('Polyfill not loaded!');
}
Check PHP Version:
composer validate --strict
Isolate Issues: Test polyfills in a fresh Laravel install:
composer create-project laravel/laravel test-polyfill
cd test-polyfill
composer require symfony/polyfill-php73
Performance:
hrtime is ~10x more precise than microtime(true) but adds ~0.1ms overhead.hrtime results for benchmarking:
static $start = hrtime(true);
Laravel-Specific:
array_key_first in Illuminate\Support\Arr helpers:
Arr::firstKey($array, $key, $default);
Future-Proofing:
composer remove symfony/polyfill-php73
Extension Points:
function array_key_first(array $array): mixed {
// Custom logic
}
require __DIR__.'/vendor/autoload.php';
CI/CD:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['7.2', '7.4', '8.1']
How can I help you explore Laravel packages today?