承接 zero-config/clone 相关项目开发

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

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

zero-config/clone

Composer 安装命令:

composer require zero-config/clone

包简介

Perform deep clones of objects.

README 文档

README

The zero-config/clone package is meant to support developers creating deep clones of objects. Contrary to most solutions out there, this package does exactly what is says on the tin and requires no knowledge of the inner workings or specific configuration in order to do what one expects it to do. 🐑

Bitbucket Pipelines codecov Packagist PHP from Packagist Packagist

Installation

composer require --dev zero-config/clone

Usage

To use the clone functionality, simply invoke the deepClone function. Alternatively, the alias 🐑 can be used.

<?php
/** @var object $original */
$copy = deepClone($original);
$copy = 🐑($original);

For an object oriented approach, one can use an instance of Cloner.

<?php
use ZeroConfig\Cloner\Cloner;

/** @var object $original */
$cloner = new Cloner();
$copy   = $cloner($original);

Rules of thumb

  1. Objects are cloned recursively.
  2. Properties of all visibilities (public, protected, private) are cloned.
  3. Static properties are NOT cloned.
  4. Singletons remain singletons.
  5. Relations are kept in-tact. References to siblings will also be referenced between cloned siblings.
  6. Cloning does not cause recursion errors when object pairs reference each other.

Proof of concept

The cloner creates deep copies of objects, recognizing references and capable of properly handling recursive self-references.

<?php
// Define the timezone for Holland and Amsterdam during winter.
$winter = new stdClass();
$winter->amsterdam = new stdClass();
// Holland inherits from Amsterdam.
$winter->holland = $winter->amsterdam;

// Set the timezone offset for Amsterdam.
$winter->amsterdam->offset = '+1';

// Create a deep clone of the timezone to set the summer configuration.
$summer = 🐑($winter);

// Set the offset for Holland.
$summer->holland->offset = '+2';

echo json_encode(
    [
        'winter' => $winter,
        'summer' => $summer
    ],
    JSON_PRETTY_PRINT
) . PHP_EOL;

The code above will output:

{
    "winter": {
        "amsterdam": {
            "offset": "+1"
        },
        "holland": {
            "offset": "+1"
        }
    },
    "summer": {
        "amsterdam": {
            "offset": "+2"
        },
        "holland": {
            "offset": "+2"
        }
    }
}

This proves that holland and amsterdam are both the same objects and when performing a deep copy, both amsterdam and holland no longer affect the child objects of the original timezone, yet they both still point to the same object.

This is achieved by checking whether a given object already has a clone, reusing the clone when available.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固