承接 kenny1911/php-clone-with 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 with construct 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

统计信息

  • 总下载量: 2.58k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-07