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. 🐑
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
- Objects are cloned recursively.
- Properties of all visibilities (public, protected, private) are cloned.
- Static properties are NOT cloned.
- Singletons remain singletons.
- Relations are kept in-tact. References to siblings will also be referenced between cloned siblings.
- 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.
zero-config/clone 适用场景与选型建议
zero-config/clone 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「object」 「clone」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zero-config/clone 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zero-config/clone 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zero-config/clone 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Runn Me! Value Objects Library
Laravel SDK mapping data in object
Traits gathering fluent syntax common methods
A fork of the great myclabs/DeepCopy bundle for Symfony
Collection of tools to use the full power of the Enyalius framework
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-03