Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Quickly convert the PHP 8 codebase to use PHP 8.1 features

The release of PHP’s latest version 8.1 is around the corner and there’s never a better time to upgrade your codebase to PHP’s latest and greated version yet!

PHP 8.1 will come with many new features such as…

…And many more!

So, you might be interested in converting your existing codebase (preferably based on PHP 8) to use PHP 8.1. Aren’t you?

Previosly, I covered how you can quickly convert your legacy codebase to use PHP 8 features using an open-source library called Rector. I would highly recommend check out the article first since it will act as a basis of the current article. Also, you might not want to jump to, let’s say PHP 7.4 to PHP 8.1 directly.

Upgrade to PHP 8 first!

You may like: How to upgrade legacy codebase to use PHP 8.0 features using Rector

Upgrading from PHP 8 to PHP 8.1

First install and configure Rector as a development dependency in your project.

Next, as I mentioned in that article, Rector uses something called rules to upgrade the PHP codebase.

We’ll update the rector.php (which will be in the project’s root) to use PHP 8.1 specific rule sets. To do so, replace the content of rector.php with the following.



use Rector\Php81\Rector\ClassConst\FinalizePublicClassConstantRector;
use Rector\Php81\Rector\FunctionLike\IntersectionTypesRector;
use Rector\Php81\Rector\Class_\MyCLabsClassToEnumRector;
use Rector\Php81\Rector\MethodCall\MyCLabsMethodCallToEnumConstRector;
use Rector\Php81\Rector\ClassMethod\NewInInitializerRector;
use Rector\Php81\Rector\FuncCall\Php81ResourceReturnToObjectRector;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Php81\Rector\Class_\SpatieEnumClassToEnumRector;

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
    $services = $containerConfigurator->services();









Start upgrading to PHP 8.1

Once the required configuration is done, you’re ready to start upgrading to PHP 8.1.

Next, you need to run the following command to apply all the PHP 8.1 related changes. To see what changes would be applied, we can run this command with the --dry-run option like so.

$ vendor/bin/rector process src --dry-run

Here src is the folder that you want to be analyzed by Rector. Once run, the command will show diff and all the rules it used.

If you’re satisfied with the potential changes, you can finally apply all changes in real by dropping --dry-run from the previous command and run it like so.

$ vendor/bin/rector process src

And that’s how you can upgrade about any codebase to PHP 8.1 in a cinch!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a


👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.