3v4l.org

run code in 300+ PHP versions simultaneously
<?php function closure_to_str($func) { $refl = new \ReflectionFunction($func); // get reflection object $path = $refl->getFileName(); // absolute path of php file $begn = $refl->getStartLine(); // have to `-1` for array index $endn = $refl->getEndLine(); $dlim = "\n"; // or PHP_EOL $list = explode($dlim, file_get_contents($path)); // lines of php-file source $list = array_slice($list, ($begn-1), ($endn-($begn-1))); // lines of closure definition $last = (count($list)-1); // last line number if((substr_count($list[0],'function')>1)|| (substr_count($list[0],'{')>1) || (substr_count($list[$last],'}')>1)) { throw new \Exception("Too complex context definition in: `$path`. Check lines: $begn & $endn."); } $list[0] = ('function'.explode('function',$list[0])[1]); $list[$last] = (explode('}',$list[$last])[0].'}'); return implode($list,$dlim); } $dog2 = ['bark'=>function($foo = '{'){}]; echo closure_to_str($dog2['bark'])."\n\n";
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.15, 8.1.0 - 8.1.2
Fatal error: Uncaught Exception: Too complex context definition in: `/in/CEmqV`. Check lines: 23 & 23. in /in/CEmqV:15 Stack trace: #0 /in/CEmqV(25): closure_to_str(Object(Closure)) #1 {main} thrown in /in/CEmqV on line 15
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'Exception' with message 'Too complex context definition in: `/in/CEmqV`. Check lines: 23 & 23.' in /in/CEmqV:15 Stack trace: #0 /in/CEmqV(25): closure_to_str(Object(Closure)) #1 {main} thrown in /in/CEmqV on line 15
Process exited with code 255.

preferences:
42.04 ms | 461 KiB | 5 Q