3v4l.org

run code in 300+ PHP versions simultaneously
<?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 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Inside: 3f3fbdf1b2226ff3eb08f8e8f6dd1d87 <br>Outside: 3f3fbdf1b2226ff3eb08f8e8f6dd1d87 <br>Inside: 5b5ecac84caac9f40efe587b26de92df <br>Outside: 5b5ecac84caac9f40efe587b26de92df <br>
Output for 5.1.0 - 5.1.6, 5.2.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.3.11, 4.4.0 - 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.

preferences:
292.68 ms | 401 KiB | 460 Q