symfony/polyfill-php82
Provides forward compatibility for PHP 8.2 features on older PHP versions. Includes polyfills for new functions and behavior changes so Symfony and other libraries can run consistently across environments without requiring an immediate PHP upgrade.
Installation: Add the package via Composer:
composer require symfony/polyfill-php82
No additional configuration is required—it auto-registers via Composer’s autoloader.
First Use Case: Use PHP 8.2+ runtime features (e.g., JSON_THROW_ON_ERROR, FILTER_VALIDATE_BOOL, or odbc_connection_string_quote()) in your Laravel code without version checks. The polyfill handles compatibility transparently.
Verification: Test on your target PHP versions (e.g., 7.4, 8.0, 8.1) to ensure features like:
json_encode($data, JSON_THROW_ON_ERROR)filter_var($input, FILTER_VALIDATE_BOOL)mb_str_split($string, 1, 'UTF-8')
work as expected.Write Laravel logic using PHP 8.2+ features directly. Example:
// In a Laravel service or controller
$json = json_encode($data, JSON_THROW_ON_ERROR); // Works on PHP <8.2
$isValid = filter_var($input, FILTER_VALIDATE_BOOL); // Polyfilled constant
If your package uses PHP 8.2+ runtime features but targets older PHP:
// composer.json
"require": {
"symfony/polyfill-php82": "^1.38"
}
This ensures downstream users get compatibility automatically.
Leverage polyfilled features in:
Random\Engine\Secure for cryptographic operations.@SensitiveParameter (if using Symfony components).FILTER_VALIDATE_BOOL or ini_parse_quantity().For legacy systems:
$quoted = odbc_connection_string_quote($connectionString); // Polyfilled
$quantity = ini_parse_quantity('100%', INI_ALL); // Works on older PHP
Use PHPUnit to validate behavior across versions:
public function testJsonThrowOnError() {
$this->expectException(JsonException::class);
json_encode([], JSON_THROW_ON_ERROR);
}
readonly properties, true typehints, or enum will fail on PHP <8.2. Verify your code’s minimum PHP version supports all syntax.class User {
public function __construct(public readonly string $name) {} // Fails
}
JSON_THROW_ON_ERROR fails).
Fix:
symfony/polyfill-php82 is installed:
composer show symfony/polyfill-php82
symfony/polyfill in phpinfo()).php artisan cache:clear
// app/Helpers/PolyfillHelper.php
class PolyfillHelper {
public static function safeJsonEncode($data, int $flags = 0) {
return json_encode($data, $flags);
}
}
Random\Engine: For advanced use cases (e.g., custom engines), consider arokettu/random-polyfill.odbc_connection_string_* functions.ini_parse_quantity() may behave differently for malformed strings.phpunit.xml to test multiple PHP versions:
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite_memory"/>
<ini name="memory_limit" value="1024M"/>
</php>
sail test --php=7.4
sail test --php=8.0
vendor/autoload.php is loaded (Laravel handles this by default).composer.json autoload section is included.JSON_THROW_ON_ERROR), remove the custom implementation to avoid conflicts. The Symfony polyfill takes precedence.How can I help you explore Laravel packages today?