A library for PHP 5.3+ that can be used to serialize closures (anonymous functions).

Installs: 966

Dependents: 6

Stars: 22

Watchers: 4

Forks: 5

Open Issues: 1

Language: PHP

2.1.0 2015-09-30 12:11 UTC


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
  • Doesn't use eval for closure serialization or unserialization
  • Works with any PHP version that has support for closures (Yes, even with PHP 5.3)
  • 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 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
  • Supports serialization of bounded objects and scopes (available only from PHP >= 5.4)


Opis Closure is licensed under the MIT License (MIT).


  • PHP 5.3.* or higher


This library is available on Packagist and can be installed using Composer.

    "require": {
        "opis/closure": "^2.1.0"

If you are unable to use Composer you can download the tar.gz or the zip archive file, extract the content of the archive and include de autoload.php file into your project.

require_once 'path/to/closure-2.1.0/autoload.php';


Examples and documentation can be found at http://opis.io/closure .