simondeeley/type
Composer 安装命令:
composer require simondeeley/type
包简介
Immuatble object behaviour for PHP
关键字:
README 文档
README
This package contains a few reusable classes, interfaces and traits which have been written to provide a basis for creating both immutable and variable objects.
This package was born out of a love creating beautifully crafted 'boilerplate' code which can be reused again and again in any number of both simple and complex objects. Most of the ideas used in this package revolve around the need to create the most basic of PHP objects - one that is immutable and unchanging. Trouble is, PHP has a fair few 'magic methods' which, although great for method overriding, are less great for ensuring an immutable state. This package aims to solve that problem and provide a number of base classes to allow you to build easy-to-use immutable classes of your own.
Requirements
- PHP >= 7.1.0
Installation
composer require simondeeley/type
Usage
Create your own immutable object
use simondeeley\ImmutableObject; class Foo extends ImmutableObject { //... }
This is the starting point to creating an immutable object. Behind the scenes, the base class of ImmutableObject sets up a few sensible defaults including overriding PHP's magic methods __set and __unset to prevent implicitly setting (or unsetting) of class properties.
There is one further method that you need to implement in your concrete classes which is getType. This method is inherited from the interface simondeeley\Type\Type which defines one static method getType which should return a string describing the type of the object.
It might seem strange having this method but it's useful when defining a bunch of objects which might, for example, share the same base class but who's identity you want to differentiate.
In the example above, you might implement the interface as such:
public static function getType(): string { return 'foo'; }
Going Further
As well as ImmutableObject this package also provides a second base class for objects which need to behave like arrays, implementing PHP's built-in ArrayAccess. This behaviour is provided in ImmutableArrayTypeObject which extends ImmutableObject and includes default implementations of offsetSet and offsetUnset.
Each of these classes uses a few traits which provide the default implementations described above. You can explore these as well as the interfaces in the code which is fully documented and annotated.
Testing Equality
Oftentimes there is a need to check that two objects are equal. This package provides a TypeEquality interface which describes one method, equals.
public bool equals ( Type $type [, int $flags ] )
This method takes an object of type simondeeley\Type\Type as an argument. This interface is the base interface that all other 'types' are built on. This method also optionally accepts a further parameter - a bitwise integer flag. This can be used to enforce or ignore certain constraints. Currently, two flags are supported:
-
TypeEquality::IGNORE_OBJECT_TYPESet this flag to ignore the type of the object which is set by
Type::getType. -
TypeEquality::IGNORE_OBJECT_IDENTITYSet this flag to ignore checking that an object points to the same PHP reference, for example by checking the value obtained by invoking
spl_object_hash.
Bitwise operators can be combined by using the pipe '|' operator, for example $foo->equals($bar, TypeEquality::IGNORE_OBJECT_TYPE | TypeEquality::IGNORE_OBJECT_IDENTITY)
When implementing this method you can choose whether to utilise the bitwise flags but they should be present in your method signature. The body of the method should then perform whatever actions are necessary to compare the two objects to determine equality. An example of this can be found in the simondeeley\Tuple package which uses this package to build an immutable tuple object that can check equality against another tuple.
simondeeley/type 适用场景与选型建议
simondeeley/type 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 458 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「immutable」 「behaviour」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simondeeley/type 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simondeeley/type 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simondeeley/type 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple, immutable data structures
Doctrine annotation to make entities undeletable.
Establishes polymorphic relations
Versioning behaviour for eloquent models
Sortable behaviour for eloquent models
Stk Immutable Data objects
统计信息
- 总下载量: 458
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0
- 更新时间: 2017-11-04