定制 tiny-blocks/immutable-object 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

tiny-blocks/immutable-object

最新稳定版本:1.0.1

Composer 安装命令:

composer require tiny-blocks/immutable-object

包简介

Provides immutable behavior for objects.

README 文档

README

License

Overview

The Immutable Object library ensures that objects implementing it remain immutable after initialization. Once created, the state of the object cannot be modified. Any attempt to change properties or collection elements will throw an exception.

Installation

composer require tiny-blocks/immutable-object

How to use

The library provides the Immutable interface and the Immutability trait to guarantee immutability. These components prevent properties and collections from being modified or unset.

Concrete implementation

By implementing the Immutable interface and using the Immutability trait, you can ensure that object properties and elements in collections are immutable.

<?php namespace Example; use TinyBlocks\Immutable\Immutable; use TinyBlocks\Immutable\Immutability; final class Order implements Immutable { use Immutability; public function __construct(public int $id, public Products $products) { } }

Handling property immutability

The Immutability trait also prevents modifications to properties of an object. Trying to modify a property after the object is initialized will throw an exception.

$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']); $order->id = 2; # Throws an exception unset($order->id); # Throws an exception 

Handling collection immutability

The Immutability trait also prevents the modification of collection elements (e.g., arrays). Trying to modify or remove an element will throw an exception.

$order = new Order(id: 1, products: new Products(array: ['item1', 'item2']); $order->items[0] = 'item3'; # Throws an exception unset($order->items[0]); # Throws an exception

License

Immutable Object is licensed under MIT.

Contributing

Please follow the contributing guidelines to contribute to the project.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固