mentors

mentors Git Source Tree


Root/vendor/jeremeamia/SuperClosure/demo/factorial.php

<?php

if (version_compare(PHP_VERSION, '5.4', '<=')) {
    throw new \RuntimeException('PHP 5.4+ is required for this example.');
}

require __DIR__ . '/../vendor/autoload.php';

use Jeremeamia\SuperClosure\SerializableClosure;

$factorial = new SerializableClosure(function ($n) use (&$factorial) {
    return ($n <= 1) ? 1 : $n * $factorial($n - 1);
});

echo $factorial(5) . PHP_EOL;
//> 120

$serialized = serialize($factorial);
$unserialized = unserialize($serialized);

echo $unserialized(5) . PHP_EOL;
//> 120

Archive Download this file

Branches

Number of commits:
Page rendered in 0.10930s using 11 queries.