Error when run "setup:di:compile" in Magento ver. 2.4.5-p1, PHP 8.1

Error when run "setup:di:compile" in Magento ver. 2.4.5-p1, PHP 8.1

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Error when run "setup:di:compile" in Magento ver. 2.4.5-p1, PHP 8.1

Solving the “setup:di:compile” Error in Magento 2.4.5-p1 with PHP 8.1

Hello Kobi,

Thank you for reaching out regarding the error you’re experiencing while trying to compile your Magento store after updating to version 2.4.5-p1 and PHP 8.1. I’ll guide you through solving this issue step-by-step.

Understanding the Error

The error you encountered is due to syntax changes introduced in PHP 8.1, which have not been fully accommodated by the current version of Magento’s setup scripts. The readonly keyword used in the error message is one such change that triggered the issue.

Step 1: Revert to a Suitable PHP Version

To temporarily resolve this, I recommend reverting your PHP version back to a version lower than 8.1. You can do this by contacting your hosting provider or using a tool like Docker if you’re running Magento locally.

If you have access to the command line, try switching to PHP 7.4 using a tool like Docker Compose:

version=7.4
docker-compose -f docker-compose.yml run --rm dev composer update

Then re-run your setup:di:compile command and see if the error persists.

Step 2: Install a Magento Patch

If reverting to an earlier PHP version is not feasible, you can apply a hotfix provided by Magento. Please check the Magento 2.4.5-p1 Release Notes and look for any available patches.

To apply a patch:

  1. Download the patch file from the official Magento site.
  2. Run the following command to apply it:
composer require magento/patch:dev-master --no-update

Replace magento/patch with the actual patch name if provided in the release notes.

Step 3: Delete and Reinstall Vendor Files

If both previous steps fail, try deleting the contents of your vendor directory:

rm -rf vendor/*

Then clear the cache using the following commands:

bin/magento cache:flush
composer clearcache

Finally, re-run composer update to reinstall all packages.

Step 4: Contact Magento Support

If none of the above steps work, it’s time to reach out to Magento’s official support. Provide them with the full error log and explain the steps you’ve taken so far. They can offer more specific guidance based on your exact setup.

Conclusion

In summary, this issue is a known incompatibility between PHP 8.1 and the current Magento setup scripts. To resolve it:

  • Revert to an earlier PHP version if possible.
  • Install any available patches provided by Magento.
  • Delete and reinstall vendor files as a last resort.
  • If all else fails, contact Magento support for direct assistance.

Remember to always backup your data before making significant changes like this. And keep an eye on the Magento Release Notes for updates fixing this issue in future versions.

Let me know if you have any further questions or need additional help!

Magento 2 Database Security Deployment

Our network