symfony/polyfill-php83
Symfony Polyfill for PHP 8.3 features on older runtimes. Adds json_validate, Override attribute, mb_str_pad, str_increment/str_decrement, updated LDAP/stream context signatures, Date/SQLite3 exception classes, and more.
Installation: Add the package via Composer in your Laravel project:
composer require symfony/polyfill-php83
Or include it via the symfony/polyfill meta-package if you already use other Symfony polyfills.
First Use Case:
Use json_validate for JSON payload validation in Laravel request handling:
use Symfony\Polyfill\Php83\JsonValidate;
$jsonPayload = request()->json()->all();
$jsonString = json_encode($jsonPayload);
$isValid = JsonValidate::validate($jsonString);
if (!$isValid) {
return response()->json(['error' => 'Invalid JSON'], 400);
}
Where to Look First:
vendor/symfony/polyfill-php83).Validation Layer:
Use json_validate in Laravel middleware or form requests:
use Symfony\Polyfill\Php83\JsonValidate;
public function handle($request, Closure $next)
{
if ($request->isJson()) {
$json = $request->getContent();
if (!JsonValidate::validate($json)) {
return response()->json(['error' => 'Invalid JSON'], 400);
}
}
return $next($request);
}
String Manipulation:
Replace custom versioning logic with str_increment/str_decrement:
use Symfony\Polyfill\Php83\StringFunctions;
$version = 'v1.0';
$nextVersion = StringFunctions::str_increment($version); // Returns 'v1.1'
Error Handling: Use modern exception classes for cleaner error propagation:
use Symfony\Polyfill\Php83\DateTimeException;
try {
$date = new DateTime('invalid');
} catch (DateTimeException $e) {
Log::error('DateTime error: ' . $e->getMessage());
}
LDAP/SQLite: Polyfill deprecated functions for backward compatibility:
use Symfony\Polyfill\Php83\LdapFunctions;
$ldapResource = LdapFunctions::ldap_connect_wallet(...);
Gradual Migration:
DateTimeException).Testing:
public function testJsonValidation()
{
$this->assertTrue(JsonValidate::validate('{"valid": true}'));
$this->assertFalse(JsonValidate::validate('invalid json'));
}
HttpTests) to validate polyfill behavior in API endpoints.Configuration:
Laravel Service Providers: Bind polyfill classes in a service provider if needed:
public function register()
{
$this->app->singleton('jsonValidator', function () {
return new class {
public function validate(string $json): bool
{
return JsonValidate::validate($json);
}
};
});
}
Custom Helpers:
Create helper methods in Laravel’s app/Helpers or a service class:
namespace App\Helpers;
use Symfony\Polyfill\Php83\StringFunctions;
class StringHelper
{
public static function incrementVersion(string $version): string
{
return StringFunctions::str_increment($version);
}
}
IDE Support: Ensure your IDE recognizes polyfill classes by configuring autoloading:
File > Settings > PHP > Include Path and add vendor/autoload.php.False Assumptions:
str_increment/str_decrement with non-string inputs or large numbers.json_validate with malformed JSON (e.g., trailing commas).Performance Overhead:
json_validate in high-traffic APIs) using Laravel’s bench() or Blackfire.IDE/Tooling Issues:
# phpstan.neon
parameters:
bootstrapFiles:
- vendor/autoload.php
PHP Version Conflicts:
composer.json:
"config": {
"platform": {
"php": "8.1"
}
}
Override Attribute:
Override attribute may cause issues with Laravel’s autoloading or IDE tooling. Ensure your IDE supports Symfony’s polyfill attributes.Polyfill-Specific Errors:
try {
$result = StringFunctions::str_increment($input);
} catch (\Throwable $e) {
Log::error('Polyfill error: ' . $e->getMessage());
$result = fallbackLogic($input);
}
Logging:
Log::debug('Using polyfill str_increment for input:', ['input' => $input]);
Testing Edge Cases:
$this->assertFalse(JsonValidate::validate(''));
$this->assertFalse(JsonValidate::validate(null));
Autoloading:
vendor/autoload.php is included in your Laravel project. If using Laravel’s bootstrap/app.php, this is handled automatically.Composer Dependencies:
composer.json:
"require": {
"symfony/polyfill-php83": "^1.38"
}
Laravel Mix/Webpack:
Custom Polyfills:
class CustomStringFunctions extends StringFunctions
{
public static function str_increment(string $str): string
{
// Custom logic
return parent::str_increment($str);
}
}
Feature Flags:
config system to toggle polyfill usage:
if (config('app.use_polyfills')) {
$result = JsonValidate::validate($json);
} else {
$result = customJsonValidate($json);
}
Polyfill Removal:
config to detect PHP version and disable polyfills:
if (version_compare(PHP_VERSION, '8.3.0', '<')) {
// Use polyfills
} else {
// Use native PHP 8.3 features
}
Service Container:
$this->app->bind(JsonValidate::class, function () {
return new class {
public function validate(string $json): bool
{
return \Symfony\Polyfill\Php83\JsonValidate::validate($json);
}
};
});
Artisan Commands:
use Symfony\Polyfill\Php83\StringFunctions;
class Increment
How can I help you explore Laravel packages today?