Readonly classes are one of the best things to happen to business code in PHP. A booking, an invoice line or a customer address gets set once and can't be changed by accident later.
The awkward part has always been the legitimate change. A customer moves their booking, or a quote gets accepted. You don't edit the old object. You make a new one with one thing different.
PHP 8.5, released in November 2025, adds a tidy way to do exactly that. It's called clone with.
What changed
clone can now be called like a function, with an optional second argument: an array of property names and new values.
$copy = clone($booking, ['hours' => 6]);
PHP makes a copy of the object, then sets just the properties you listed. Everything else is carried across as it was. The old clone $booking syntax still works exactly as before, so nothing existing breaks.
The details are in the Clone with v2 RFC and the PHP 8.5 release notes.
Why it matters for value objects
The usual pattern for an immutable object is a "with-er" method: withHours(), withStatus() and so on. Before 8.5, a readonly class had to rebuild itself through the constructor:
return new static($this->customer, $this->date, $hours, $this->status);
That's fine with four properties. With fifteen, every with-er repeats every property, in order. Add a property later and miss one method, and you've got a bug that only shows up when that method runs.
In PHP 8.5 the same method is one line, and it doesn't care how many other properties the class has:
return clone($this, ['hours' => $hours]);
Less repetition means fewer places for mistakes to hide. For a small business site that takes bookings or produces invoices, that's the whole point.
The rules worth knowing
- Readonly is only relaxed during the clone, and only for the properties you pass in. Afterwards the new object is just as locked as the old one.
- Visibility still applies. A public readonly property is
protected(set)by default, so code outside the class gets an error if it triesclone($booking, ['hours' => 6]). Changes have to go through the class's own with-ers, which is what you want. __clone()runs first, then the new values are assigned in the order you list them. If one assignment fails, the rest are not applied.- Types and property hooks behave like a normal assignment. Give an int property a word like "six" and you get a TypeError.
- Misspelt names fail loudly in readonly classes. PHP throws "Cannot create dynamic property" rather than quietly adding a new one.
- It works as a callable too, so
array_map(clone(...), $items)copies every object in an array.
The gotcha: the constructor is skipped
Cloning has never called the constructor, and clone with doesn't either. If your constructor checks that a session is 3 or 6 hours, that check is not repeated when you clone.
On PHP 8.5.8, a with-er that cloned without its own check happily produced a booking with 9 hours. So keep the business rule in the with-er as well, ideally in one private method that both the constructor and the with-ers call:
if (!in_array($hours, [3, 6], true)) { throw new InvalidArgumentException('Sessions are 3 or 6 hours.'); }
Type the with-er parameters too, and keep declare(strict_types=1); at the top of the file, so a string can't slip in at the method boundary.
Is it worth adopting?
If your site runs PHP 8.5 and uses readonly value objects, yes. It's a small change with a real payoff: shorter classes, with-ers that say what they do, and one less way to get things wrong.
If you're not sure which PHP version you're on, check that first. Our PHP 8.2 end-of-life guide shows how, and why the clock is ticking.
For a lighter take on the same feature, there's a version on Ian's blog.
Readonly value objects, enums, strict types and tested examples run all the way through PHP Done The Correct Way. The companion code page has the examples, and the book page has the details.
If it makes money, it makes sense. Code that changes only what it should is a good place to start.