/** * Returns whether this is an internal method * * @return boolean True if this is an internal method */ public function isInternal() { if ($this->reflectionSource instanceof ReflectionMethod) { return $this->reflectionSource->isInternal(); } else { return parent::isInternal(); } }
/** * @param ReflectionMethod $method * @return string */ function generateMethodArguments(ReflectionMethod $method) { if ($method->isInternal()) { // This code can`t handle constants, it replaces its with value return implode(', ', array_map(array($this, 'generateMethodArgument'), $method->getParameters())); } else { $lines = $this->getFileLines($method->getFileName()); $start_line = $method->getStartLine() - 1; $function_lines = array_slice($lines, $start_line, $method->getEndLine() - $start_line); // todo: use code beautifier? return $this->parseFunctionArgs(implode(PHP_EOL, $function_lines)); } }
function reflectMethod($class, $method) { $methodInfo = new ReflectionMethod($class, $method); echo "**********************************\n"; echo "Reflecting on method {$class}::{$method}()\n\n"; echo "\ngetName():\n"; var_dump($methodInfo->getName()); echo "\nisInternal():\n"; var_dump($methodInfo->isInternal()); echo "\nisUserDefined():\n"; var_dump($methodInfo->isUserDefined()); echo "\n**********************************\n"; }
/** * Handles request * * @return void */ public function handle() { if ($_SERVER['REQUEST_METHOD'] != 'POST') { die('JSON-RPC Server'); } try { $request = json_decode(file_get_contents('php://input')); $reflection = new ReflectionMethod($this->_object, $request->method); if ($reflection->getNumberOfRequiredParameters() > count($request->params)) { throw new Exception("Params count doesn't match method signature."); } if (!$reflection->isPublic() || $reflection->isInternal()) { throw new Exception("Method is not callable"); } $result = call_user_func_array(array($this->_object, $request->method), $request->params); $response = array('id' => $request->id, 'result' => $result, 'error' => null); } catch (Exception $e) { $response = array('id' => $request->id, 'result' => null, 'error' => $e->getMessage()); } header('content-type: text/javascript'); echo json_encode($response); }
function methodData(ReflectionMethod $method) { $details = ""; $name = $method->getName(); if ($method->isUserDefined()) { $details .= "{$name} is user defined\n"; } if ($method->isInternal()) { $details .= "{$name} is built-in\n"; } if ($method->isAbstract()) { $details .= "{$name} is abstract\n"; } if ($method->isPublic()) { $details .= "{$name} is public\n"; } if ($method->isProtected()) { $details .= "{$name} is protected\n"; } if ($method->isPrivate()) { $details .= "{$name} is private\n"; } if ($method->isStatic()) { $details .= "{$name} is static\n"; } if ($method->isFinal()) { $details .= "{$name} is final\n"; } if ($method->isConstructor()) { $details .= "{$name} is the constructor\n"; } if ($method->returnsReference()) { $details .= "{$name} returns a reference (as opposed to a value)\n"; } return $details; }
<pre> <?php class Counter { private static $c = 0; public static final function increment() { return ++self::$c; } } // Создание экземпляра класса ReflectionMethod $method = new ReflectionMethod('Counter', 'increment'); // exit; // Вывод основной информации printf("===> %s%s%s%s%s%s%s метод '%s' (который является %s)\n" . " объявлен в %s\n" . " строки с %d по %d\n" . " имеет модификаторы %d[%s]\n", $method->isInternal() ? 'Встроенный' : 'Пользовательский', $method->isAbstract() ? ' абстрактный' : '', $method->isFinal() ? ' финальный' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' статический' : '', $method->getName(), $method->isConstructor() ? 'конструктором' : 'обычным методом', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers()))); // Вывод статических переменных, если они есть if ($statics = $method->getStaticVariables()) { printf("---> Статическая переменная: %s\n", var_export($statics, 1)); } // Вызов метода printf("---> Результат вызова: "); $result = $method->invoke(3); echo $result; ?> </pre>
class Counter { private static $c = 0; /** * Increment counter * * @final * @static * @access public * @return int */ public static final function increment() { return ++self::$c; } } // Create an instance of the ReflectionMethod class $method = new ReflectionMethod('Counter', 'increment'); // Print out basic information printf("===> The %s%s%s%s%s%s%s method '%s' (which is %s)\n" . " declared in %s\n" . " lines %d to %d\n" . " having the modifiers %d[%s]\n", $method->isInternal() ? 'internal' : 'user-defined', $method->isAbstract() ? ' abstract' : '', $method->isFinal() ? ' final' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' static' : '', $method->getName(), $method->isConstructor() ? 'the constructor' : 'a regular method', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers()))); echo "\n"; // Print documentation comment printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); // Print static variables if existant if ($statics = $method->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } echo "\n"; // Invoke the method printf("---> Invocation results in: "); var_dump($method->invoke(NULL));
function methodData(ReflectionMethod $method) { $details = ""; $name = $method->getName(); if ($method->isUserDefined()) { $details .= "{$name} -- метод определён пользователем<br>"; } if ($method->isInternal()) { $details .= "{$name} -- внутренний метод<br>"; } if ($method->isAbstract()) { $details .= "{$name} -- абстрактный метод<br>"; } if ($method->isPublic()) { $details .= "{$name} -- публичный метод<br>"; } if ($method->isProtected()) { $details .= "{$name} -- защищенный метод<br>"; } if ($method->isPrivate()) { $details .= "{$name} -- закрытый метод метод<br>"; } if ($method->isStatic()) { $details .= "{$name} -- статичный метод<br>"; } if ($method->isFinal()) { $details .= "{$name} -- финальный метод<br>"; } if ($method->isConstructor()) { $details .= "{$name} -- метод конструктора<br>"; } if ($method->returnsReference()) { $details .= "{$name} -- метод возвращает ссылку а не значение<br>"; } return $details; }
} catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } try { new ReflectionMethod('a', 'b', 'c'); } catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } class C { public function f() { } } $rm = new ReflectionMethod('C', 'f'); var_dump($rm->isFinal(1)); var_dump($rm->isAbstract(1)); var_dump($rm->isPrivate(1)); var_dump($rm->isProtected(1)); var_dump($rm->isPublic(1)); var_dump($rm->isStatic(1)); var_dump($rm->isConstructor(1)); var_dump($rm->isDestructor(1)); var_dump($rm->getModifiers(1)); var_dump($rm->isInternal(1)); var_dump($rm->isUserDefined(1)); var_dump($rm->getFileName(1)); var_dump($rm->getStartLine(1)); var_dump($rm->getEndLine(1)); var_dump($rm->getStaticVariables(1)); var_dump($rm->getName(1));
function transformMethod(ReflectionMethod $method) { $is_internal = $method->isInternal() ? 'internal' : 'user-defined'; $is_abstract = $method->isAbstract() ? ' abstract' : ''; $is_final = $method->isFinal() ? ' final' : ''; $is_public = $method->isPublic() ? ' public' : ''; $is_private = $method->isPrivate() ? ' private' : ''; $is_protected = $method->isProtected() ? ' protected' : ''; $is_static = $method->isStatic() ? ' static' : ''; $name = $method->getName(); $class = $method->getDeclaringClass()->name; $is_constructor = $method->isConstructor() ? 'the constructor' : 'a regular method'; $modifiers = Reflection::getModifierNames($method->getModifiers()); extract($this->transformDocBlock($method->getDocComment())); $parameters = array(); $parameters_concat = ""; $_optional_count = 0; foreach ($method->getParameters() as $_param) { $parameters[$_param->getName()] = $this->transformParameter($_param, @$param[$_param->getName()]); if ($_param->isOptional()) { $parameters_concat .= " [ "; $_optional_count++; } if ($parameters_concat != "" && $parameters_concat != " [ ") { $parameters_concat .= ", "; } $parameters_concat .= $parameters[$_param->getName()]; } $parameters_concat .= str_repeat(" ] ", $_optional_count); ob_start(); include "templates/" . $this->template . "/method.tmpl.php"; return ob_get_clean(); }