symfony/polyfill-php81
Symfony Polyfill for PHP 8.1 features on older runtimes. Adds array_is_list, enum_exists, MYSQLI_REFRESH_REPLICA, ReturnTypeWillChange, and CURLStringFile (PHP 7.4+). Drop-in Composer dependency for wider compatibility.
Installation:
composer require symfony/polyfill-php81
For development/testing only:
composer require --dev symfony/polyfill-php81
First Use Case:
Replace manual array validation with array_is_list in Laravel’s validation logic (e.g., app/Http/Requests/):
use Symfony\Polyfill\Php81\Php81;
public function rules()
{
return [
'data' => ['required', function ($attribute, $value, $fail) {
if (!Php81\array_is_list($value)) {
$fail('The '.$attribute.' must be a sequential array.');
}
}],
];
}
Where to Look First:
array_keys($array) === range(0, count($array) - 1) with Php81\array_is_list($array).enum_exists() to check for custom enums (e.g., PaymentStatus::class).MYSQLI_REFRESH_REPLICA or CURLStringFile in database/HTTP layers.Validation Layer:
FormRequest or Validator classes.// Before
if (!is_array($data) || array_keys($data) !== range(0, count($data) - 1)) {
return false;
}
// After
return Php81\array_is_list($data);
Domain-Driven Design (DDD):
enum_exists() to validate domain enums before PHP 8.1 upgrade.if (Php81\enum_exists('App\Enums\UserRole')) {
$role = new UserRole('ADMIN');
}
HTTP/File Uploads:
CURLStringFile for file uploads in legacy Laravel branches.$curlFile = new \Symfony\Polyfill\Php81\Php81\CURLStringFile(
file_get_contents('/path/to/file'),
'image/png',
'filename.png'
);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $curlFile]);
Deprecation Prep:
@return void and ReturnTypeWillChange to prepare for PHP 8.1+./**
* @return void
* @deprecated Use new method instead
*/
public function legacyMethod()
{
# ReturnTypeWillChange annotation (handled by polyfill)
return 'string'; // Will be void in PHP 8.1+
}
Incremental Adoption:
Testing Strategy:
$this->partialMock(Php81::class, ['array_is_list'])
->method('array_is_list')
->willReturn(false);
Performance Profiling:
Php81\array_is_list() vs. native array_is_list() in PHP 8.1+.Laravel Service Providers:
MYSQLI_REFRESH_REPLICA) in AppServiceProvider:
public function boot()
{
define('MYSQLI_REFRESH_REPLICA', \Symfony\Polyfill\Php81\Php81\MYSQLI_REFRESH_REPLICA);
}
Custom Helpers:
trait UsesPhp81Polyfills
{
protected function isList(array $array): bool
{
return \Symfony\Polyfill\Php81\Php81\array_is_list($array);
}
}
CI/CD Pipeline:
- name: Test with Polyfill
run: composer require symfony/polyfill-php81 && php artisan test
Documentation:
/**
* @param array $array Must be a sequential list (checked via Php81\array_is_list)
*/
public function process(array $array): void
PHP Version Mismatches:
CURLStringFile on PHP <7.4 will throw errors (polyfill checks runtime PHP version).composer.json enforces PHP ≥7.4:
"config": {
"platform": {
"php": "7.4"
}
}
Enum Behavior Differences:
enum_exists() may return true for invalid enums in polyfilled environments.if (Php81\enum_exists('App\Enums\Status') && class_exists('App\Enums\Status')) {
$enum = new Status();
}
Performance Overhead:
// Custom array_is_list (faster than polyfill for some cases)
function is_list(array $array): bool {
return array_keys($array) === range(0, count($array) - 1);
}
Laravel 10+ Conflicts:
composer remove symfony/polyfill-php81
ReturnTypeWillChange Misuse:
private function internalMethod()
{
// ReturnTypeWillChange (safe for internal use)
return 'string'; // Will be void in PHP 8.1+
}
Polyfill Not Loading:
Class 'Symfony\Polyfill\Php81\Php81' not found.composer dump-autoload
composer.json includes the package and run composer install --no-dev.Method Not Found Errors:
Call to undefined method Php81::array_is_list().use Symfony\Polyfill\Php81\Php81; // Correct
// vs.
use Symfony\Polyfill\Php81; // Incorrect (missing \Php81)
Runtime PHP Version Checks:
CURLStringFile fails with "PHP version not supported".var_dump(PHP_VERSION_ID >= 70400); // Must be true
Alias for Cleaner Code:
config/app.php:
'aliases' => [
'Php81' => Symfony\Polyfill\Php81\Php81::class,
],
if (Php81::array_is_list($data)) { ... }
Laravel Artisan Commands:
use Symfony\Polyfill\Php8
How can I help you explore Laravel packages today?