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); } $dog = ['bark'=>function(){}]; $dog2 = ['bark'=>function($foo = '{'){}]; echo closure_to_str($dog['bark'])."\n\n"; echo closure_to_str($dog2['bark'])."\n\n";
Output for 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
Fatal error: Uncaught TypeError: implode(): Argument #1 ($separator) must be of type string, array given in /in/1UCQd:20 Stack trace: #0 /in/1UCQd(20): implode(Array, '\n') #1 /in/1UCQd(26): closure_to_str(Object(Closure)) #2 {main} thrown in /in/1UCQd on line 20
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30
Fatal error: Uncaught TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in /in/1UCQd:20 Stack trace: #0 /in/1UCQd(20): implode(Array, '\n') #1 /in/1UCQd(26): closure_to_str(Object(Closure)) #2 {main} thrown in /in/1UCQd on line 20
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Deprecated: implode(): Passing glue string after array is deprecated. Swap the parameters in /in/1UCQd on line 20 function(){} Fatal error: Uncaught Exception: Too complex context definition in: `/in/1UCQd`. Check lines: 24 & 24. in /in/1UCQd:15 Stack trace: #0 /in/1UCQd(27): closure_to_str(Object(Closure)) #1 {main} thrown in /in/1UCQd on line 15
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33
function(){} Fatal error: Uncaught Exception: Too complex context definition in: `/in/1UCQd`. Check lines: 24 & 24. in /in/1UCQd:15 Stack trace: #0 /in/1UCQd(27): closure_to_str(Object(Closure)) #1 {main} thrown in /in/1UCQd on line 15
Process exited with code 255.

preferences:
117.05 ms | 1522 KiB | 4 Q