Last active
February 9, 2017 10:48
-
-
Save lastguest/215993bc7876c7a8bb127e8c5940919e to your computer and use it in GitHub Desktop.
[Closure Source] Get Closure source code. #tags: PHP, reflection, source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function closure_source(Closure $c) { | |
$rfx = new ReflectionFunction($c); $args = []; | |
foreach($rfx->getParameters() as $p) | |
$args[] = ($p->isArray() ? 'array ' : ($p->getClass() ? $p->getClass()->name . ' ' : '')) | |
. ($p->isPassedByReference() ? '&' : '') . '$' . $p->name | |
. ($p->isOptional() ? ' = ' . var_export($p->getDefaultValue(), true) : ''); | |
return 'function(' . implode(',', $args) . "){\n" | |
. implode('',array_slice(file($rfx->getFileName()), | |
$s=$rfx->getStartLine(),$rfx->getEndLine()-$s-1)).'}'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Approach is good but it wont work with
closure_source(function() {echo 'one liner test';});