We are using Magento 2.4.5-p1 and php 8.1 version, upgrade command is working fine but when we try to run the di-compile command getting an error of

Repositories code generation... 1/9 [===>------------------------]  11% < 1 sec 139.0 MiBsyntax error, unexpected token "readonly", expecting identifier#0 /var/www/html/project_name/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php(117): Magento\Setup\Module\Di\Code\Reader\ClassesScanner->includeClass()
#1 /var/www/html/project_name/setup/src/Magento/Setup/Module/Di/Code/Reader/ClassesScanner.php(87): Magento\Setup\Module\Di\Code\Reader\ClassesScanner->extract()
#2 /var/www/html/project_name/setup/src/Magento/Setup/Module/Di/App/Task/Operation/RepositoryGenerator.php(61): Magento\Setup\Module\Di\Code\Reader\ClassesScanner->getList()
#3 /var/www/html/project_name/setup/src/Magento/Setup/Module/Di/App/Task/Manager.php(56): Magento\Setup\Module\Di\App\Task\Operation\RepositoryGenerator->doOperation()
#4 /var/www/html/project_name/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php(216): Magento\Setup\Module\Di\App\Task\Manager->process()
#5 /var/www/html/project_name/vendor/symfony/console/Command/Command.php(255): Magento\Setup\Console\Command\DiCompileCommand->execute()
#6 /var/www/html/project_name/vendor/symfony/console/Application.php(1021): Symfony\Component\Console\Command\Command->run()
#7 /var/www/html/project_name/vendor/symfony/console/Application.php(275): Symfony\Component\Console\Application->doRunCommand()
#8 /var/www/html/project_name/vendor/magento/framework/Console/Cli.php(116): Symfony\Component\Console\Application->doRun()
#9 /var/www/html/project_name/vendor/symfony/console/Application.php(149): Magento\Framework\Console\Cli->doRun()
#10 /var/www/html/project_name/bin/magento(23): Symfony\Component\Console\Application->run()
#11 {main}

Also, this issue is also in GitHub but the not fully proper response - https://github.com/magento/magento2/issues/36521

Does anyone know how to resolve this?

  • It's just Permission issue. Dec 26, 2022 at 6:50
  • @BaharuniAsif i also tried to give permission to my project but still getting an issue Dec 26, 2022 at 6:55

3 Answers 3


As you can see, the original poster of the issue in the Github issue mentioned a custom module causing his issue.

PHP 8.1 introduces readonly as a reserved PHP keyword.
So find in your custom code or 3rd-party extensions to see if any file using readonly is invalid then fix it. Run this command: grep -r --include \*.php --exclude-dir=vendor/magento readonly app vendor

  • I replaced third party extension and the problem was resolved Dec 26, 2022 at 13:40

Issue belongs to vendor file. Please remove and update vendor files by composer. Backup current vendor folder first then remove by using below steps:

  1. rm -rf vendor/*
  2. composer update/install. It will download latest files and the try after give all required permissions.

It may help you!



You can detect the problematic module easily by adding a little bit of debug code to the vendor/composer/ClassLoader.php file. In the findFile class, arround line 447, add a var_dump($class); like so:

public function findFile($class)
    // class map lookup

Then run bin/magento setup:di:compile. the last class printed before the fatal error is your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.