Menu

← Back to property.*

Error Identifier: property.writeOnly

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

/**
 * @property-write int $writeOnlyProperty
 */
#[\AllowDynamicProperties]
class Foo
{
	public function doFoo(): void
	{
		echo $this->writeOnlyProperty;
	}
}

Why is it reported? #

The code reads a property that is declared as write-only via the @property-write PHPDoc tag. A write-only property is designed to accept values but not to be read. Reading it is not allowed because the class does not guarantee a meaningful value when the property is accessed.

How to fix it #

If the property needs to be both readable and writable, use @property instead of @property-write:

 /**
- * @property-write int $writeOnlyProperty
+ * @property int $writeOnlyProperty
  */
 #[\AllowDynamicProperties]
 class Foo

If the property should remain write-only, restructure the code to avoid reading it. Use a different property or method to expose the value:

 /**
  * @property-write int $writeOnlyProperty
+ * @property-read int $readableProperty
  */
 #[\AllowDynamicProperties]
 class Foo
 {
 	public function doFoo(): void
 	{
-		echo $this->writeOnlyProperty;
+		echo $this->readableProperty;
 	}
 }

How to ignore this error #

You can use the identifier property.writeOnly to ignore this error using a comment:

// @phpstan-ignore property.writeOnly
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: property.writeOnly

Rules that report this error #

  • PHPStan\Rules\Properties\ReadingWriteOnlyPropertiesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.