3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class ClosureHash { /** * List of hashes * * @var SplObjectStorage */ protected static $hashes = null; /** * Returns a hash for closure * * @param callable $closure * * @return string */ public static function from(Closure $closure) { if (!self::$hashes) { self::$hashes = new SplObjectStorage(); } if (!isset(self::$hashes[$closure])) { $ref = new ReflectionFunction($closure); $file = new SplFileObject($ref->getFileName()); $file->seek($ref->getStartLine()-1); $content = ''; while ($file->key() < $ref->getEndLine()) { $content .= $file->current(); $file->next(); } self::$hashes[$closure] = md5(json_encode(array( $content, $ref->getStaticVariables() ))); } return self::$hashes[$closure]; } } class Test { public function hello($greeting) { $closure = function ($message) use ($greeting, &$closure) { echo "Inside: ", ClosureHash::from($closure), PHP_EOL, "<br>" ; }; return $closure; } } $obj = new Test(); $closure = $obj->hello('Hello'); $closure('PHP'); echo "Outside: ", ClosureHash::from($closure), PHP_EOL, "<br>"; $another = $obj->hello('Bonjour'); $another('PHP'); echo "Outside: ", ClosureHash::from($another), PHP_EOL, "<br>";
Output for 5.3.0 - 7.1.0
Inside: 3f3fbdf1b2226ff3eb08f8e8f6dd1d87 <br>Outside: 3f3fbdf1b2226ff3eb08f8e8f6dd1d87 <br>Inside: 5b5ecac84caac9f40efe587b26de92df <br>Outside: 5b5ecac84caac9f40efe587b26de92df <br>
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION in /in/IDT6m on line 46
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION in /in/IDT6m on line 46
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IDT6m on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IDT6m on line 9
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/IDT6m on line 9
Process exited with code 255.