eloquent/equality
最新稳定版本:2.1.2
Composer 安装命令:
composer require eloquent/equality
包简介
A better strict comparison for PHP.
README 文档
README
A better strict comparison for PHP.
Deprecated
Equality is deprecated. Please use Parity instead.
Installation and documentation
- Available as Composer package eloquent/equality.
- API documentation available.
The problem
Sometimes it is necessary to compare two objects to determine whether they are considered equal.
If the == operator is used, there is no strictness about the equality. For
instance, this snippet outputs 'equal':
$left = new stdClass; $left->foo = 0; $right = new stdClass; $right->foo = null; if ($left == $right) { echo 'equal'; } else { echo 'not equal'; }
Conversely, if the === operator is used, objects are not equal unless they are
the same instance. The following snippet outputs 'not equal':
$left = new stdClass; $left->foo = 'bar'; $right = new stdClass; $right->foo = 'bar'; if ($left === $right) { echo 'equal'; } else { echo 'not equal'; }
Unfortunately PHP does not have an inbuilt method to compare objects strictly without requiring that they be the same instance. This is where Equality comes in. This snippet correctly outputs 'equal':
use Eloquent\Equality\Comparator; $left = new stdClass; $left->foo = 'bar'; $right = new stdClass; $right->foo = 'bar'; $comparator = new Comparator; if ($comparator->equals($left, $right)) { echo 'equal'; } else { echo 'not equal'; }
Usage
Equality is very simple to use. Simply instantiate a Comparator and use its
equals() method:
use Eloquent\Equality\Comparator; $comparator = new Comparator; if ($comparator->equals($left, $right)) { // equal } else { // not equal }
Equality can work with any PHP data type, not just objects.
Custom equality logic
In some cases it may be desirable to customize how equality is determined.
The interface EqualityComparable can be used to provide a custom equality
implementation for any class:
use Eloquent\Equality\Comparator; use Eloquent\Equality\EqualityComparable; class Foo implements EqualityComparable { /** * @param mixed $value * @param Comparator $comparator * * @return boolean */ public function isEqualTo($value, Comparator $comparator) { // custom logic... } }
When Equality encounters an object that implements EqualityComparable, it
will return the result of the isEqualTo() method instead of using the default
equality logic. The comparator itself will be passed as the second parameter.
How does Equality work?
Equality uses reflection to recurse over the values it is passed and ensure that they are deeply, and strictly, equal.
In addition, it implements special protections to avoid infinite recursion issues, such as objects that contain themselves, or objects that contain the object that they are being compared to.
eloquent/equality 适用场景与选型建议
eloquent/equality 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.56k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2012 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「object」 「equality」 「comparison」 「strict」 「deep」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eloquent/equality 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eloquent/equality 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eloquent/equality 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides the functionality to compare PHP values for equality
A customizable deep comparison library.
A set of useful PHP classes.
Interfaces and functions for type-safe, consistent value comparison
Runn Me! Value Objects Library
A simple tool to check your i18n translation files to make sure they are complete across all languages
统计信息
- 总下载量: 8.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-11-08