PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP 8.5 Clone With: Modifying Properties When Cloning Objects Works
PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields.
Compatibility note: Requires PHP 8.5 or later.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
final readonly class Address { public function __construct(public string $city, public string $country) {} }
$originalAddress = new Address("Paris", "France");
$updatedAddress = clone($originalAddress, ["city" => "Lyon"]);
echo $updatedAddress->city;
PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
final readonly class Price { public function __construct(public int $cents, public string $currency) {} }
$originalPrice = new Price(1500, "EUR");
$discountedPrice = clone($originalPrice, ["cents" => 1200]);
echo $originalPrice->cents . " -> " . $discountedPrice->cents;
Clone-with replaces properties on the copy after __clone() and must respect visibility and property types.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
final readonly class Invoice { public function __construct(public string $number, public string $status) {} public function withStatus(string $status): self { return clone($this, ["status" => $status]); } }
$invoice = new Invoice("INV-15", "draft");
echo $invoice->withStatus("sent")->status;
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- Clone-with replaces properties on the copy after __clone() and must respect visibility and property types.
- PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP 8.5 Clone With: Modifying Properties When Cloning Objects | PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields. |
| Alternative approach | A manual withX() constructor can provide copy-on-write behavior on earlier PHP versions. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP 8.5 Clone With: Modifying Properties When Cloning Objects | Clone-with replaces properties on the copy after __clone() and must respect visibility and property types. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | A manual withX() constructor can provide copy-on-write behavior on earlier PHP versions. |
Key Takeaways
- PHP 8.5 adds clone($object, $properties), creating a copy and overriding selected properties, including readonly fields.
- Clone-with replaces properties on the copy after __clone() and must respect visibility and property types.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP 8.5 Clone With: Modifying Properties When Cloning Objects is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.