承接 its-mieger/obj 相关项目开发

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

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

its-mieger/obj

Composer 安装命令:

composer require its-mieger/obj

包简介

Library for implementing native data type behaviour (comparision, conversion, ...) for objects

README 文档

README

This library helps to implement behavior like native data types (casts, comparision, conversion, operator overloading ...) for custom PHP objects.

Since PHP does not support operator overloading, custom helper functions are implemented to apply operations using the implemented behavior.

Following interfaces are available:

Interface Purpose
Comparable Object can be compared with others (overrides comparision operators)
CastsAsBoolean Object can be represented as boolean
CastsAsFloat Object can be represented as float
CastsAsInteger Object can be represented as integer
CastsAsString Object can be represented as string
OperatorAdd Object implements custom add operator
OperatorSubtract Object implements custom subtract operator
OperatorMultiply Object implements custom multiply operator
OperatorDivide Object implements custom divide operator
Nullable Object can represent null

Helper functions

Since PHP does not support operator overloading special helper functions must be used to make objects behave as implemented by the specified interfaces. However helper functions are not autoloaded but may be imported by calling Obj::loadHelpers(). If not loaded, the helpers may be called using the static methods of the Obj-class

Native Helper function Call without helper function
== o_eq Obj::equal(...)
!= o_ne Obj::notEqual(...)
<=> o_comp Obj::compare(...)
< o_lt Obj::lessThan(...)
<= o_le Obj::lessThanOrEqual(...)
> o_gt Obj::greaterThan(...)
>= o_ge Obj::greaterThanOrEqual(...)
+ o_add Obj::add(...)
- o_sub Obj::subtract(...)
* o_mul Obj::multiply(...)
/ o_div Obj::divide(...)
isFloat() o_isFloat Obj::isFloat(...)
isInt() o_isInt Obj::isInt(...)
isBool() o_isBool Obj::isBool(...)
isString() o_isString Obj::isString(...)
(float) o_castFloat Obj::castFloat(...)
(int) o_castInt Obj::castInt(...)
(bool) o_castBool Obj::castBool(...)
(string) o_castString Obj::castString(...)
o_comparePipe Obj::comparePipe(...)

Behavior

Using one of the object helper functions, objects behave like native data types would do for the corresponding native operator.

Example:

class MyInt implements CastsAsInteger {
 	
 	public function toInt(): int {
 		return 3;
 	}
 	
}
$a = new MyInt();

$result = op_add($a, 2);

In this example $result will be 5. Adding an integer to the object will call the toInt()-function to cast the object as integer and than execute the operation.

Important You should not implement more than one CastsAs*-interface in one class. Of you would do the object might behave unexpected since in some situations it might be converted eg. to int and in others to float. Rather than implementing both interface, you should only implement eg. int. If a float value is required, the returned int value is casted to float automatically.

Overriding operators

Sometimes casting objects is not enough. Imagine a custom Money implementation where you want to add two Money objects and want to receive a new Money object instead of a scalar number. Therefore you may implement one of the Operator*-interfaces and define your custom operator.

How operators are applied

Sometimes it is not obvious which operator function is called. Especially if having two objects implementing the same operator. The behavior described below concerns the Operator*-interfaces as well as the Comparable-interface which overrides all comparision operators with one function:

  • If both implement the operator interface, the operator function is invoked for the descended object if the objects are descendants and the operation is commutative (the compare operation is also treated as commutative). If objects are not descendants, the operator function is invoked for the left side object.

  • If only one implements the Operator interface, the operator function is invoked for the corresponding object.

  • If none implements the operator interface the objects are converted to their native representation and the native operator is applied.

Testing

You may mock helpers by creating a mock object for the ObjectHelper-class and pass it to Obj::mock(...). This way all calls to object helpers (via helper functions or static interface) are redirected to the mocked object. Call Obj::resetMock() to remove the mocked object und make object logic be used again.

its-mieger/obj 适用场景与选型建议

its-mieger/obj 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 07 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 its-mieger/obj 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 its-mieger/obj 我们能提供哪些服务?
定制开发 / 二次开发

基于 its-mieger/obj 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: proprietary
  • 更新时间: 2020-07-15