Opis Closure

序列化闭包(匿名函数)。「Serialize closures (anonymous functions).」

Github stars Tracking Chart

Opis Closure

可序列化的闭包

Opis Closure 是一个旨在克服 PHP 在闭包序列化方面的限制的库,它提供了一个可以使所有闭包都可序列化的包装器。

该库的主要特点:
  • 序列化任何闭包(closure)
  • 序列化任意对象
  • 不使用 eval 进行闭包序列化或反序列化
  • 可与任何支持闭包的 PHP 版本一起使用
  • 支持 PHP 7 语法
  • 处理 use() 中引用/导入的所有变量,并自动打包所有引用/导入的闭包以进行正确的序列化
  • 处理递归闭包
  • 处理诸如__FILE__, __DIR__, __LINE__, __NAMESPACE__, __CLASS__, __TRAIT__, __METHOD__ 和 __FUNCTION__ 等魔术常量。
  • 自动解析闭包内使用的所有类名、函数名和常量名
  • 使用 #trackme 指令跟踪闭包的驻留源
  • 简单且非常快速的解析器
  • 执行未序列化的闭包时可能发生的任何错误或异常,都可以捕获并正确处理
  • 您可以无限期序列化/反序列化任何闭包,甚至包括先前未序列化的次数(这是可能的,因为 eval() 不用于反序列化)
  • 处理静态闭包
  • 支持加密签名的关闭
  • 提供一个反射器,可以为您提供有关序列化闭包的信息
  • 提供 SuperClosure 库的分析器

自动检测何时需要序列化闭包的范围和/或绑定对象,以使闭包在反序列化后正常工作

文档

可以在这里找到该库的完整文档。

许可

Opis Closure 已根据 MIT License (MIT) 获得许可。

要求

PHP ^5.4 || ^7.0

安装

Opis Closure 在 Packagist 上可用,可以使用 Composer 从命令行界面安装。

Composer require opis/closures

或者您可以直接将其作为依赖项引用到 composer.json 文件中

{
    "require": {
        "opis/closure": "^3.5"
    }
}

从2.x迁移

如果您的项目需要支持PHP 5.3,则可以继续使用Opis Closure的2.x版本。 否则,假设您未使用已删除/重构的类或功能之一(请参阅 CHANGELOG),则迁移到版本3.x只是更新 composer.json 文件的问题。

语义版本控制

Opis Closure 遵循 语义版本控制规范

任意对象序列化

我们添加这个特性是为了支持闭包绑定对象的序列化。实现还远远不够完美,要使它完美地工作真的很难。我们将努力改进,但我们不能保证什么。因此,我们对Opis\Closure\serialize|unserialize 函数的建议是谨慎使用它们。


Main metrics

Overview
Name With Owneropis/closure
Primary LanguagePHP
Program languagePHP (Language Count: 1)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2014-02-10 17:49:53
Pushed At2025-07-08 20:38:31
Last Commit At2025-01-10 23:50:13
Release Count63
Last Release Name3.7.0 (Posted on )
First Release Name1.0.0 (Posted on 2014-02-13 00:42:14)
用户参与
Stargazers Count2.5k
Watchers Count24
Fork Count91
Commits Count406
Has Issues Enabled
Issues Count86
Issue Open Count0
Pull Requests Count34
Pull Requests Open Count0
Pull Requests Close Count37
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Opis Closure

Build Status
Latest Stable Version
Latest Unstable Version
License

Serializable closures

Opis Closure is a library that aims to overcome PHP's limitations regarding closure
serialization by providing a wrapper that will make all closures serializable.

The library's key features:

  • Serialize any closure
  • Serialize arbitrary objects
  • Doesn't use eval for closure serialization or unserialization
  • Works with any PHP version that has support for closures
  • Supports PHP 7 syntax
  • Handles all variables referenced/imported in use() and automatically wraps all referenced/imported closures for
    proper serialization
  • Handles recursive closures
  • Handles magic constants like __FILE__, __DIR__, __LINE__, __NAMESPACE__, __CLASS__,
    __TRAIT__, __METHOD__ and __FUNCTION__.
  • Automatically resolves all class names, function names and constant names used inside the closure
  • Track closure's residing source by using the #trackme directive
  • Simple and very fast parser
  • Any error or exception, that might occur when executing an unserialized closure, can be caught and treated properly
  • You can serialize/unserialize any closure unlimited times, even those previously unserialized
    (this is possible because eval() is not used for unserialization)
  • Handles static closures
  • Supports cryptographically signed closures
  • Provides a reflector that can give you information about the serialized closure
  • Provides an analyzer for SuperClosure library
  • Automatically detects when the scope and/or the bound object of a closure needs to be serialized
    in order for the closure to work after deserialization

Documentation

The full documentation for this library can be found [here][documentation].

License

Opis Closure is licensed under the [MIT License (MIT)][license].

Requirements

  • PHP ^5.4