terablaze/serializable-closure

Terablaze Serializable Closure provides an easy and secure way to serialize closures in PHP.

dev-master / 1.x-dev 2023-01-20 21:30 UTC

This package is auto-updated.

Last update: 2024-12-21 02:00:16 UTC


README

Build Status License

Introduction

This project is a fork laravel/serializable-closure package, which is a fork of the excellent opis/closure: 3.x package.

Terablaze Serializable Closure provides an easy and secure way to serialize closures in PHP.

Official Documentation

Installation

Requires PHP 7.4+

First, install Terablaze Serializable Closure via the Composer package manager:

composer require terablaze/serializable-closure

Usage

You may serialize a closure this way:

use Terablaze\SerializableClosure\SerializableClosure;

$closure = fn () => 'james';

// Recommended
SerializableClosure::setSecretKey('secret');

$serialized = serialize(new SerializableClosure($closure));
$closure = unserialize($serialized)->getClosure();

echo $closure(); // james;

Caveats

  1. Creating anonymous classes within closures is not supported.
  2. Using attributes within closures is not supported.
  3. Serializing closures on REPL environments such as Terablaze Repl is not supported.

License

Serializable Closure is open-sourced software licensed under the MIT license.