Function Kenny1911\CloneWith\clone_with can set values of properties during the cloning process.
In future versions of php, there may be a
clone withconstruct that will solve the same problem.
use function Kenny1911\CloneWith\clone_with;
class Post
{
/** @var string */
private $title;
/** @var string */
private $author;
public function __construct(string $title, string $author)
{
$this->title = $title;
$this->author = $author;
}
public function getTitle(): string
{
return $this->title;
}
public function getAuthor(): string
{
return $this->author;
}
}
$post = new Post('Foo', 'Author');
$post2 = clone_with($post, ['title' => 'Bar']);
echo $post2->getTitle(); // BarReadonly properties appeared in PHP 8.1.
It values cannot be changed, if use standard way to cloning objects (using clone operator).
Function clone_with supports override values of readonly object properties.
Classes can has __clone() method, that contains additional logic of cloning object instance.
Function clone_with supports it and clone logic will not be violated.
- spatie/php-cloneable - A
Cloneabletrait that allows you to clone readonly properties in PHP 8.1.