Object Cloning — a practical guide to PHP clone object with clear examples you can reuse in real projects.
OOP Tutorial Series (40/65). Prefer one article? Read the complete OOP tutorial.
Short description
Default cloning is shallow. Implement `__clone` for deep copies of nested objects.
clone + __clone
class Cart {
public function __construct(public array $items) {}
public function __clone(): void {
// adjust cloned state if needed
}
}
$a = new Cart(['book']);
$b = clone $a;