kenny1911/php-clone-with
最新稳定版本:v1.0.2
Composer 安装命令:
composer require kenny1911/php-clone-with
包简介
Helper for cloning objects with other properties
README 文档
README
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.
Usage
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(); // Bar
Cloning PHP 8.1 readonly properties
Readonly 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.
Support classes with __clone() method
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.
Alternatives
- spatie/php-cloneable - A
Cloneabletrait that allows you to clone readonly properties in PHP 8.1.
统计信息
- 总下载量: 2.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-07