phpgears/immutability 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

phpgears/immutability

Composer 安装命令:

composer require phpgears/immutability

包简介

Object immutability guard for PHP

README 文档

README

PHP version Latest Version License

Build Status Style Check Code Quality Code Coverage

Total Downloads Monthly Downloads

Object immutability guard for PHP

Truly object immutability in PHP is an impossible task by the nature of the language itself, carefully checking and protecting properties and methods visibility is the closest we can get at this moment in time

Crafting an object to make it as immutable as can be requires expertise, dedication and focus so no mutating mechanism slips through developer's hands into the object, that is the reason behind this library, provide a way to help with the tedious task of ensuring object immutability

Installation

Composer

composer require phpgears/immutability

Usage

Require composer autoload file

require './vendor/autoload.php';

Gears\Immutability\ImmutabilityBehaviour trait enforces you to avoid public properties and mutable methods in your class by calling assertImmutable method on object construction, __wakeup method and unserialize methods if Serializable interface is implemented

This mentioned behaviour let alone would run your objects completely useless, you must provide an implementation of the abstract method getAllowedInterfaces, returning a list of interfaces whose public methods will be allowed in your class. Although you can also provide class names, as it can prove useful in some special cases, it is highly discouraged and should be used sparsely

Few PHP magic methods are allowed to be defined as public, namely __construct, __destruct, __get, __isset, __sleep, __wakeup, __serialize, __unserialize, __toString, __set_state, __clone, __debugInfo

use Gears\Immutability\ImmutabilityBehaviour;

interface MyInterface extends \Serializable
{
    /**
     * @return string
     */
    public function getParameter(): string;
}

final class MyClass implements MyInterface
{
    use ImmutabilityBehaviour;

    protected function __construct()
    {
        $this->assertImmutable();
    }

    /**
     * Static methods are allowed.
     */
    public static function instance(): self
    {
        return new self();
    }

    /**
     * Method allowed because it's defined in the interface.
     */
    public function getParameter(): string
    {
        return '';
    }

    /**
     * Method allowed because it's defined in the \Serializable.
     */
    public function serialize(): string
    {
        return '';
    }

    /**
     * Method allowed because it's defined in the \Serializable.
     */
    public function unserialize($serialized): void
    {
        $this->assertImmutable();

        // unserialize
    }

    /**
     * {@inheritdoc}
     */
    protected function getAllowedInterfaces(): array
    {
        return [MyInterface::class];
    }
}

As a general rule, your immutable classes should be declared final. In case you really need inheritance, at least your implementation of getAllowedInterfaces method has to be declared final for every instantiable class

Although not mandatory it is advised to make your constructors protected and force developers to create static named constructors

From now on you can focus on those methods defined in your interfaces and make sure they do not mutate your object, with the confidence no other developer would mutate your object by accident or intentionally

Example

If you want to have a look at a working example implementations of immutable objects have a look at phpgears/identity, phpgears/dto or phpgears/cqrs

Contributing

Found a bug or have a feature request? Please open a new issue. Have a look at existing issues before.

See file CONTRIBUTING.md

License

See file LICENSE included with the source code for a copy of the license terms.

phpgears/immutability 适用场景与选型建议

phpgears/immutability 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.18k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 09 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「object」 「immutability」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 phpgears/immutability 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 7.18k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

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

其他信息

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