symfony/polyfill-php73
Symfony Polyfill for PHP 7.3: provides missing core features on older PHP versions, including array_key_first/last, hrtime, is_countable, and JsonException. Part of Symfony’s polyfill set for forward-compatible apps.
Installation:
composer require symfony/polyfill-php73
First Use Case:
Replace legacy array traversal with array_key_first in a Laravel controller or service:
// Before (PHP <7.3)
$key = array_search('target', $array);
$firstKey = reset(array_keys($array));
// After (works on PHP ≥7.2 with polyfill)
$firstKey = array_key_first($array);
Verify Compatibility:
Array Traversal:
array_key_first/array_key_last:
// Eloquent example: Find first key in a collection
$firstId = array_key_first($user->roles->toArray());
JSON Error Handling:
JsonException for robust JSON parsing (e.g., API responses):
try {
$data = json_decode($response, false, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
Log::error('Invalid JSON: ' . $e->getMessage());
return response()->json(['error' => 'Invalid data'], 400);
}
High-Resolution Timing:
hrtime (e.g., middleware execution):
$start = hrtime(true);
// ... code to measure ...
$duration = hrtime(true) - $start;
Log::debug("Execution time: {$duration} nanoseconds");
Countable Checks:
is_array + count with is_countable:
if (is_countable($items)) {
$count = count($items);
}
Phased Migration:
# Add to composer.json
composer require symfony/polyfill-php73
# Remove after PHP 7.3+ upgrade
composer remove symfony/polyfill-php73
Laravel Package Development:
// Package code (works on PHP 7.2+ with polyfill)
public function getFirstKey(array $array): ?string
{
return array_key_first($array);
}
Cross-Environment Consistency:
// Works identically on all environments
$key = array_key_first($config['settings']);
Leverage Laravel’s Collections: Combine with Laravel’s collection methods for cleaner syntax:
$firstKey = $collection->keys()->first();
// Or with polyfill:
$firstKey = array_key_first($collection->toArray());
CI/CD Enforcement: Add PHP version checks to prevent accidental deployment on unsupported versions:
# GitHub Actions example
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '7.2'
Static Analysis: Use PHPStan/Psalm to detect unused polyfills (indicating no runtime impact):
vendor/bin/phpstan analyse --level=5
Redundant Polyfills:
symfony/polyfill via dependencies. Check:
composer show symfony/polyfill
PHP Version Mismatch:
docker run --rm -it php:7.2-cli composer show symfony/polyfill-php73
Custom Polyfill Conflicts:
array_key_first) elsewhere in the codebase. Audit with:
grep -r "array_key_first" app/
Performance Overhead:
$start = microtime(true);
array_key_first($largeArray);
$time = microtime(true) - $start;
Verify Polyfill Activation: Check if polyfills are loaded:
if (function_exists('array_key_first')) {
echo "Polyfill active (PHP <7.3)";
}
JSON Exception Handling:
Ensure JSON_THROW_ON_ERROR is supported (PHP 7.3+). Polyfill provides compatibility:
json_decode($json, false, 512, JSON_THROW_ON_ERROR); // Works with polyfill
hrtime Precision:
Note hrtime returns nanoseconds (float). Convert to milliseconds for readability:
$durationMs = (hrtime(true) - $start) / 1_000_000;
Version Pinning: Lock to a specific version to avoid surprises:
"symfony/polyfill-php73": "1.37.0"
Laravel-Specific:
Combine with Laravel’s Str::of() or collect() for cleaner syntax:
$firstKey = Str::of($array)->keys()->first();
Future-Proofing: Use feature detection to handle both polyfilled and native implementations:
if (defined('JSON_THROW_ON_ERROR')) {
json_decode($json, false, 512, JSON_THROW_ON_ERROR);
} else {
$data = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JsonException(json_last_error_msg());
}
}
Testing: Mock polyfills in tests to avoid environment dependencies:
// tests/TestCase.php
protected function setUp(): void
{
if (!function_exists('array_key_first')) {
function array_key_first(array $array): ?string
{
return key($array);
}
}
}
Removal Post-Upgrade: After upgrading PHP, remove the package and update tests:
composer remove symfony/polyfill-php73
# Update tests to remove mocks
How can I help you explore Laravel packages today?