PHP error guide

Cannot modify readonly property: meaning and fix

Error summary

Initialize it once in the declaring scope and create a new object for changed state. Code assigns a readonly property after its one permitted initialization.

What it means

Initialize it once in the declaring scope and create a new object for changed state. Code assigns a readonly property after its one permitted initialization.

What the error means

This message means that code assigns a readonly property after its one permitted initialization. The exact signature distinguishes cannot modify readonly property from a generic application failure.

Why PHP produces it

The engine or service reports “Cannot modify readonly property” because its required precondition was not met. Initialize it once in the declaring scope and create a new object for changed state.

PHP version notes

The cannot modify readonly property wording here is based on PHP 8.1+; punctuation and exception class names can differ on older branches or vendor builds.

Most common causes

  • The immediate input or configuration reaches the specific condition: code assigns a readonly property after its one permitted initialization
  • The code path assumes the prerequisite for cannot modify readonly property has already been satisfied.
  • For cannot modify readonly property, development and production differ in version, extension, permissions, paths, or service configuration.
  • An earlier operation returned an unchecked value that is consumed by the line reporting cannot modify readonly property.

Minimal examples

BAD — reproduces the problem

php
$invoice->id = 12;

FIXED — safer pattern

php
final class Invoice { public function __construct(public readonly int $id) {} }
$invoice = new Invoice(12);

Step-by-step diagnosis

  1. Copy the complete “Fatal error: Uncaught Error: Cannot modify readonly property Invoice::$id” text and retain the first application stack frame.
  2. Reproduce cannot modify readonly property in the same SAPI and environment listed for this page.
  3. Before changing cannot modify readonly property, inspect the preceding value or directive and verify its type, path, version, and permissions.
  4. Apply the narrow correction—initialize it once in the declaring scope and create a new object for changed state—then repeat the original request once.

Fixes

Correct the failing prerequisite

Initialize it once in the declaring scope and create a new object for changed state

php
final class Invoice { public function __construct(public readonly int $id) {} }
$invoice = new Invoice(12);

Fail explicitly at the boundary

Validate the condition before the operation that emits cannot modify readonly property, and log a safe diagnostic without credentials or full production paths.

Common mistakes when fixing it

  • Suppressing cannot modify readonly property instead of correcting its upstream condition.
  • Testing cannot modify readonly property only with the CLI binary when the failing request runs under FPM or Apache.
  • Changing a global setting for cannot modify readonly property before confirming the site-specific effective configuration.

How to prevent it

  • Add a focused test that exercises the boundary responsible for cannot modify readonly property.
  • Keep runtime versions, extensions, configuration, and deploy artifacts affecting cannot modify readonly property reproducible.
  • Validate external data and service return values before they can trigger cannot modify readonly property.

Web server / environment notes

cli, fpm, apache. The failure occurs where code assigns a readonly property after its one permitted initialization

Tags: cli,fpm,apache

Categories