Directly answering your question: No, you cannot mark regular properties as readonly. If you want to set primitive types (except array), that will never change, you should use constants

const QWE = '666';

That doesn't work for objects and arrays. I see two (lets say) "solutions"

  1. Use Getter

    private $qwe;
    public function getQwe() { return $this->qwe; }
    protected function setQwe($value) { $this->qwe = $value; }
    

    I don't like them very much ("Properties define the state, not the behavior, like methods do"). You always get twice as much additional methods as properties and if you have many properties, this will extremely blow up your class. However, it's as far as I can see the only way to implement what you want to achieve.

  2. Trust your users ;) Comment your property and say something like "If you change this value, probably something will break and its your very own fault".

    /**
     * QWE
     *
     * This property should be treatened as "readonly". If you change this value
     * something scary will happen to you.
     *
     * @readonly
     * @var string
     */
    public $qwe = '666';
    

    Its not great, but at least you can say "I told you".

Answer from KingCrunch on Stack Overflow
Top answer
1 of 5
5

Directly answering your question: No, you cannot mark regular properties as readonly. If you want to set primitive types (except array), that will never change, you should use constants

const QWE = '666';

That doesn't work for objects and arrays. I see two (lets say) "solutions"

  1. Use Getter

    private $qwe;
    public function getQwe() { return $this->qwe; }
    protected function setQwe($value) { $this->qwe = $value; }
    

    I don't like them very much ("Properties define the state, not the behavior, like methods do"). You always get twice as much additional methods as properties and if you have many properties, this will extremely blow up your class. However, it's as far as I can see the only way to implement what you want to achieve.

  2. Trust your users ;) Comment your property and say something like "If you change this value, probably something will break and its your very own fault".

    /**
     * QWE
     *
     * This property should be treatened as "readonly". If you change this value
     * something scary will happen to you.
     *
     * @readonly
     * @var string
     */
    public $qwe = '666';
    

    Its not great, but at least you can say "I told you".

2 of 5
3

Readonly properties were introduced in PHP 8.1, but static readonly properties are not supported.

From the RFC:

Readonly static properties are not supported. This is a technical limitation, in that it is not possible to implement readonly static properties non-intrusively. In conjunction with the questionable usefulness of readonly static properties, this is not considered worthwhile at this time.

However you can declare a static function to more or less get the same end result.

public static function getType(): string
{
    return 'Duck';
}
🌐
PHP
wiki.php.net › rfc › readonly_properties_v2
PHP: rfc:readonly_properties_v2
Readonly static properties are not supported. This is a technical limitation, in that it is not possible to implement readonly static properties non-intrusively.
🌐
Stitcher
stitcher.io › blog › readonly-classes-in-php-82
Readonly classes in PHP 8.2 | Stitcher.io
PHP 8.2 adds a new way of declaring classes: you can make them readonly. In practice, it means that all properties of that class will be readonly.
🌐
PHP.Watch
php.watch › versions › 8.1 › readonly
Readonly Properties - PHP 8.1 • PHP.Watch
It is not possible to port the readonly functionality to older PHP versions. However, the @readonly / @property-read annotations is understood by static analyzers such as Psalm.
🌐
Reddit
reddit.com › r/php › what's the benefit of readonly properties over constants?
r/PHP on Reddit: What's the benefit of readonly properties over constants?
November 16, 2024 -

After all, the overlap is so big that I struggle to see why they were introduced.

If you want a property to be immutable after assignment, a constant does that, too. That's also why constants being public is fine.

So, I would have found readonly more useful, if I was allowed to always re-assign them from inside the class that defined them. Then they would work like a private property that only has a getter but no setter - which I find convenient. It's the job of the class to manage its state, so I don't see why you shouldn't be allowed to re-assign them from inside when constants already exist.

Care to enlighten me?

🌐
dailycomputerscience
dailycomputerscience.com › post › complete-guide-on-readonly-properties-in-php
Complete Guide on Readonly Properties in PHP
October 11, 2024 - Readonly properties can only be declared with non-static properties. They promote safer code and better data encapsulation, especially in object models with constant values or data that shouldn’t change after initialization.
🌐
phpFashion
phpfashion.com › en › php-readonly-properties
The Hidden Surprises of PHP Readonly Properties » phpFashion
PHP 8.2 kicked things up a notch. Instead of securing individual properties, you can now lock down an entire class. It's like upgrading from a safe to a vault: readonly class User { public string $name; // Automatically readonly! public string $email; // Also readonly! } But hold on – this power comes with some strict rules: Every property must have a type · Static properties are off-limits ·
Find elsewhere
🌐
Mark Baker's Blog
markbakeruk.net › 2021 › 11 › 30 › readonly-gotchas
ReadOnly Gotchas – A few more limitations with PHP 8.1 ReadOnly Properties | Mark Baker's Blog
November 30, 2021 - The inability to clone an object ... mentioned in the PHP documentation, they could easily be overlooked. You cannot apply readonly to static properties....
🌐
PHP Tutorial
phptutorial.net › home › php oop › php readonly properties
PHP Readonly Properties
April 7, 2025 - In this tutorial, you'll learn about the PHP readonly properties that can be only initialized once.
🌐
Amit Merchant
amitmerchant.com › readonly-classes-in-php-82
Readonly classes in PHP 8.2
April 29, 2022 - On top of this, making a class readonly will also prevent the creation of dynamic properties on the class instance (which is also going to be deprecated in PHP 8.2). As for restrictions, like readonly properties, it’s not possible to use untyped and static properties with readonly classes.
🌐
Thephp
discourse.thephp.foundation › mailing lists › php.internals
[PHP-DEV] static properties in readonly classes - php.internals - PHP Foundation Discourse
June 8, 2025 - Hello internals, As far as I can tell from previous conversations and PRs; static properties aren’t supported in readonly classes for technical reasons and because nobody was sure what to do with them … … but that seems weird because you can also do something like this: https://3v4l.org/uYNQD to hack your way around it if you want a mutable static state encapsulated in the class.
🌐
Dogan-ucar
dogan-ucar.de › startseite › readonly classes in php: a useful addition to readonly properties
Readonly Classes in PHP: A Useful Addition to Readonly Properties
June 21, 2024 - As with readonly properties, classes declared readonly can only contain typed properties. This is required by PHP but makes apart of that much sense since it makes the code more robust. As with “Typed Properties Only”, static properties are also disallowed by PHP.