Migration to PHP 8
When you're ready to migrate to PHP 8.1+ native enums, use the php-compatible-enum-upgrade-to-php8 CLI tool.
Running the Migration
# Scan src/ directory (default)
vendor/bin/php-compatible-enum-upgrade-to-php8
# Scan a specific directory
vendor/bin/php-compatible-enum-upgrade-to-php8 app/Enums
# Preview changes without modifying files
vendor/bin/php-compatible-enum-upgrade-to-php8 --dry-run
What the Tool Does
The migration tool will:
- Convert enum class definitions - Transform
class Status extends Enumtoenum Status - Update case declarations - Convert
protected $draft;tocase draft; - Handle backed enums - Preserve integer or string backing values
- Update usages - Replace
Status::draft()withStatus::draft(removes parentheses) - Swap EnumLabel - Replace
EnumLabel::from()withPhp8EnumLabel::fromEnum()
Example Transformation
Before:
<?php
use PhpCompatible\Enum\Enum;
use PhpCompatible\Enum\EnumLabel;
class Status extends Enum
{
protected $draft;
protected $published = 10;
}
$status = Status::draft();
echo EnumLabel::from($status);
After:
<?php
use PhpCompatible\Enum\Php8EnumLabel;
enum Status: int
{
case draft = 0;
case published = 10;
}
$status = Status::draft;
echo Php8EnumLabel::fromEnum($status);
Unit Enums vs Backed Enums
The migration tool automatically determines whether to create a unit enum or a backed enum:
- Unit enum: Created when no explicit values are set (pure enum without backing type)
- Backed enum: Created when any case has an explicit value (int or string)
Unit Enum Example
Before:
class Direction extends Enum
{
protected $north;
protected $south;
protected $east;
protected $west;
}
After:
enum Direction
{
case north;
case south;
case east;
case west;
}
Backed Enum Example
Before:
class Color extends Enum
{
protected $red = 'red';
protected $green = 'green';
protected $blue = 'blue';
}
After:
enum Color: string
{
case red = 'red';
case green = 'green';
case blue = 'blue';
}
Post-Migration Steps
After running the migration tool:
- Update your
composer.json- You can removephp-compatible/enumfrom your dependencies if you no longer need backward compatibility - Update PHP requirement - Set your minimum PHP version to 8.1+
- Run your tests - Ensure all functionality works as expected
- Keep
Php8EnumLabel- If you use human-readable labels, keep the package installed forPhp8EnumLabel
tip
Always run with --dry-run first to preview changes before modifying your files!