static function getClosure($fn) { $source = new \ReflectionFunction($fn); $class = $source->getFileName(); $beginLine = $source->getStartLine(); $endLine = $source->getEndline(); return "{$class} {$beginLine} : {$endLine}"; }
/** * Show reflection * * Show reflection * * <code> * Panda_Debug::reflect('BEAR_Form'); // Class * Panda_Debug::reflect($obj); // Objecy * Panda_Debug::reflect('p'); // Function * </code> * * @param string $target target * @param boll $cehckParent check parent class * * @return void */ public static function reflect($target, $cehckParent = false) { if (is_object($target)) { $target = get_class($target); } switch (true) { case function_exists($target): $ref = new ReflectionFunction($target); $info['name'] = $ref->isInternal() ? 'The internal ' : 'The user-defined '; $info['name'] .= $targetName = $ref->getName(); $info['declare in'] = $ref->getFileName() . ' lines ' . $ref->getStartLine() . ' to ' . $ref->getEndline(); $info['Documentation'] = $ref->getDocComment(); $statics = $ref->getStaticVariables(); if ($statics) { $info['Static variables'] = $statics; } $type = 'function'; break; case class_exists($target, false): $ref = new ReflectionClass($target); $type = 'class'; $info['name'] = $ref->isInternal() ? 'The internal ' : 'The user-defined '; $info['name'] .= $ref->isAbstract() ? ' abstract ' : ''; $info['name'] .= $ref->isFinal() ? ' final ' : ''; $info['name'] .= $ref->isInterface() ? 'interface ' : 'class '; $info['name'] .= $targetName = $ref->getName(); $info['declare in'] = $ref->getFileName() . ' lines ' . $ref->getStartLine() . ' to ' . $ref->getEndline(); $info['modifiers'] = Reflection::getModifierNames($ref->getModifiers()); $info['Documentation'] = $ref->getDocComment(); $info['Implements'] = $ref->getInterfaces(); $info['Constants'] = $ref->getConstants(); foreach ($ref->getProperties() as $prop) { // ReflectionProperty クラスのインスタンスを生成する $propRef = new ReflectionProperty($targetName, $prop->name); if ($propRef->isPublic()) { $porps[] = $prop->name; } } // $info['Public Properties'] = $porps; foreach ($ref->getMethods() as $method) { $methodRef = new ReflectionMethod($targetName, $method->name); if ($methodRef->isPublic() || $method->isStatic()) { $final = $method->isFinal() ? 'final ' : ''; $pubic = $method->isPublic() ? 'public ' : ''; $static = $method->isStatic() ? ' static ' : ''; $methods[] = sprintf("%s%s%s %s", $final, $pubic, $static, $method->name); } } $info['Public Methods'] = $methods; if ($ref->isInstantiable() && is_object($target)) { $info['isInstance ?'] = $ref->isInstance($target) ? 'yes' : 'no'; } if ($parent) { $info['parent'] .= $ref->getParentClass(); } break; default: $type = 'Invalid Object/Class'; $targetName = $target; $info = null; break; } print_a($info, "show_objects:1;label: Reflection of {$type} '{$targetName}'"); }
function show_info($function, $basic = 0) { $func = new ReflectionFunction($function); $ret = ''; $ret .= "<pre><strong>Information:</strong>\n" . var_export($func->getDocComment(), 1) . "</pre>\n"; if ($basic) { $ret .= "<pre><pre>===> The " . ($func->isInternal() ? 'internal' : 'user-defined') . " function '" . $func->getName() . "'\n" . "\tdeclared in " . $func->getFileName() . "\n\tlines " . $func->getStartLine() . " to " . $func->getEndline() . "</pre>\n"; } return $ret; }
public function serializeClosure(callable $closure) { $ref = new \ReflectionFunction($closure); $file = $ref->getFileName(); $start = $ref->getStartLine(); $end = $ref->getEndline(); $content = file($file); $code = []; for ($i = $start - 1; $i < $end; $i++) { $code[] = trim($content[$i]); } $code = implode('', $code) . 'end'; $function = 'function (' . Utils::cut('function (', '});end', $code) . '}'; return $function; }
<pre> <?php function sayHello($name, $h) { static $count = 0; return "<h{$h}>Hello, {$name}</h{$h}>"; } // Обзор функции Reflection::export(new ReflectionFunction('sayHello')); // Создание экземпляра класса ReflectionFunction $func = new ReflectionFunction('sayHello'); // Вывод основной информации printf("<p>===> %s функция '%s'\n" . " объявлена в %s\n" . " строки с %d по %d\n", $func->isInternal() ? 'Internal' : 'User-defined', $func->getName(), $func->getFileName(), $func->getStartLine(), $func->getEndline()); // Вывод статических переменных, если они есть if ($statics = $func->getStaticVariables()) { printf("<p>---> Статическая переменная: %s\n", var_export($statics, 1)); } // Вызов функции printf("<p>---> Результат вызова: "); $result = $func->invoke("John", "1"); echo $result; ?> </pre>
/** */ function get_function_source($name) { $r = new ReflectionFunction($name); $info = ['name' => $r->getName(), 'file' => $r->getFileName(), 'line_start' => $r->getStartLine(), 'line_end' => $r->getEndline(), 'params' => $r->getParameters(), 'comment' => $r->getDocComment()]; $info['source'] = $this->get_file_slice($info['file'], $info['line_start'], $info['line_end']); return $info; }
static $var = 1; } $func = new ReflectionFunction("test"); var_dump($func->export("test")); echo "--getName--\n"; var_dump($func->getName()); echo "--isInternal--\n"; var_dump($func->isInternal()); echo "--isUserDefined--\n"; var_dump($func->isUserDefined()); echo "--getFilename--\n"; var_dump($func->getFilename()); echo "--getStartline--\n"; var_dump($func->getStartline()); echo "--getEndline--\n"; var_dump($func->getEndline()); echo "--getDocComment--\n"; var_dump($func->getDocComment()); echo "--getStaticVariables--\n"; var_dump($func->getStaticVariables()); echo "--invoke--\n"; var_dump($func->invoke(array(1, 2, 3))); echo "--invokeArgs--\n"; var_dump($func->invokeArgs(array(1, 2, 3))); echo "--returnsReference--\n"; var_dump($func->returnsReference()); echo "--getParameters--\n"; var_dump($func->getParameters()); echo "--getNumberOfParameters--\n"; var_dump($func->getNumberOfParameters()); echo "--getNumberOfRequiredParameters--\n";