Cleaner Classes in PHP 8.6: Readonly Property Defaults and #[\Override] on Constants cover image

Cleaner Classes in PHP 8.6: Readonly Property Defaults and #[\Override] on Constants

Scott Keck-Warren • November 27, 2026

In PHP 8.5, if you try to define a public readonly value, you'll get an error:

class Podcast {
    // PHP 8.5.6 Fatal error: Readonly property Podcast::$feedSlug cannot have default value
    public readonly string $feedSlug = 'community-corner-podcast';
}

It's something the creators of the language decided can't be supported because it's essentially a constant, and we already have CONST for this. If you want a readonly string with a default value, you have to set it in the constructor.

class Podcast {
    public readonly string $feedSlug;

    public function __construct() {
        $this->feedSlug = 'community-corner-podcast';
    }
}

It's kinda annoying because we need 3 extra lines to define our string.

Another issue that's common is you might have logic like the following where you have a constant in an interface that is overridden in an implemented class

interface Show {
    const FEED_FORMAT = 'rss';
}

class Podcast implements Show {
    const FEED_FORMAT = 'rss-2.0';
}

If you want to rename the constant in the interface, you have to pray you changed it everywhere correctly so those overridden classes still have the correct logic.

Both of these are fixed in PHP 8.6, and both make a class's contract harder to break.

Why Readonly Properties Couldn't Have Defaults

Readonly properties couldn't have defaults for a very simple reason. A readonly property with a default value isn't a property; it's a constant. So the argument is that you should use a constant. The language implemented them with the fact that you would get an error if you tried to use them in this "bad" manner.

The workaround was to define the read-only property inside our constructors like so:

class Podcast {
    public readonly string $feedSlug;

    public function __construct() {
        $this->feedSlug = 'community-corner-podcast';
    }
}

The reasoning held until 8.4, when a change was made to allow interface properties.

What Changed: Interface Properties

In PHP 8.6, the team added property hooks, which allowed the following to be defined in an interface.

interface PublishesToFeed {
    // essentially read-only
    public string $feedSlug { get; }
}

The Pre-8.6 options can't satisfy this without doing the messy constructor or reimplementing the hook.

To fix this, 8.6 adds support for default values on readonly properties.

class Podcast implements PublishesToFeed {
    // PHP 8.6 - Success!
    public readonly string $feedSlug = 'community-corner-podcast';
}

The Rules

Depending on your use case, there are still some limitations, including:

Value Objects Get Cleaner

The benefit to your codebase is in value objects. We use these everywhere, and having the ability to declare a readonly property's default is going to save a lot of boilerplate code.

That being said, there are still benefits to using a class constant, like when the value is shared across all instances, and nothing needs to read it as a property.

Which dovetails nicely into.

#[\Override] Comes to Class Constants

Way back in PHP 8.3, support was added to allow you to define a method as overriding a parent class's version.

class ParentClass
{
    public function print(): void
    {
        // do something
    }
}

class ChildClass extends ParentClass
{
    #[\Override]
    public function print(): void
    {
        // do something
    }
}

Then if you changed the name of the method in the ParentClass, it would result in an error instead of just chugging along.

PHP 8.6 extends this logic to class constants.

interface Show {
    const FEED_FORMAT = 'rss';
}

class Podcast implements Show {
    #[\Override] // valid: overrides interface constant
    const FEED_FORMAT = 'rss-2.0';
}

Now if we change the parent's name, we'll get an error in PHP 8.6:

interface Show {
    const FEED_FORMAT_NAME = 'rss';
}

class Podcast implements Show {
    // Fatal error: Podcast::FEED_FORMAT has #[\Override] attribute, 
    // but no matching parent constant exists
    #[\Override] // valid: overrides interface constant
    const FEED_FORMAT = 'rss-2.0';
}

What You Should Know

These are two small changes with a common goal. Creating class contracts that fail at compile time (hard errors) instead of just drifting (no error or confusing errors). After PHP 8.6 has been installed, search for constructors that only assign literals to readonly properties, and add #[\Override] to constants that redefine a parent's version to take full advantage of this.

Both of these features are in my PHP 8.6 roundup. You can watch the video or read the written version. Make sure you come back next week for our discussion of Time\Duration.

AI notice

The outline for this post was generated via AI, then I wrote the prose and ran it through Grammarly