public function import(\ReflectionFunction $function) { $this->name = $function->name; $this->function = $function->getShortName(); $this->namespace = $function->getNamespaceName(); $this->_importFromReflection($function); }
function definition($functionName) { $details = new \ReflectionFunction("JREAM\\{$functionName}"); $output = '<div>'; $output .= "<strong>{$details->getShortName()}</strong>( "; foreach ($details->getParameters() as $reflectObj) { foreach ($reflectObj as $arg) { $output .= "\${$arg}, "; } } $output = rtrim($output, ' ,'); $output .= " )"; $output .= '</div>'; return $output; }
/** * @param $function * @return BuildParameterArray * @throws \ReflectionException */ public static function forFunction($function) { if (!is_callable($function)) { $template = 'Error building parameters, Function {$method_name} does not exist'; $message = (new ReplaceVarsFromArray($template))->apply(['method_name' => $function]); throw new \ReflectionException($message); } // Allow for [$class, $method] callable syntax which doesn't work with // ReflectionFunction. if (is_array($function)) { list($class, $method) = $function; return static::forMethod($class, $method); } $reflection_function = new \ReflectionFunction($function); $reflection_parameters = $reflection_function->getParameters(); return new static($reflection_parameters, $reflection_function->getShortName()); }
/** * @param callable $function * * @return \ReflectionFunctionAbstract * @throws InvalidFunctionException */ public static final function fromCallable(callable $function) { // If is array it could be an instance or static method: // ['class', 'method'] or [$instance, 'method']; if (is_array($function)) { return new \ReflectionMethod($function[0], $function[1]); } elseif ($function instanceof \Closure) { $reflection = new \ReflectionFunction($function); // If the name is {closure} it as an actual closure if ($reflection->getShortName() === '{closure}') { return $reflection; } // Bail out, no (sane) way of determining the actual function // represented by the closure throw InvalidFunctionException::invalidFunctionMessage('The function has been wrapped in closure ' . '(most likely via ReflectionFunction::getClosure or \\ReflectionMethod::getClosure) ' . 'and this is not supported', $reflection); } elseif (is_object($function)) { return new \ReflectionMethod($function, '__invoke'); } else { $name = null; is_callable($function, false, $name); return new \ReflectionFunction($name); } }
<?php namespace A\B; function foo() { } $function = new \ReflectionFunction('sort'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName()); $function = new \ReflectionFunction('A\\B\\foo'); var_dump($function->inNamespace()); var_dump($function->getName()); var_dump($function->getNamespaceName()); var_dump($function->getShortName());
$staticX++; $x = $staticX; return $x; } class Test { public function test() { } } $rf = new \ReflectionFunction('\\foo\\bar\\f'); print "--- getShortName(\"\\foo\\bar\\f\") ---\n"; var_dump($rf->getShortName()); print "\n"; print "--- getNamespaceName(\"\\foo\\bar\\f\") ---\n"; var_dump($rf->getNamespaceName()); print "\n"; $rf = new \ReflectionMethod('\\foo\\bar\\Test', 'test'); print "--- getShortName(\"\\foo\\bar\\Test::test\") ---\n"; var_dump($rf->getShortName()); print "\n"; print "--- getNamespaceName(\"\\foo\\bar\\Test::test\") ---\n"; var_dump($rf->getNamespaceName()); print "\n"; $rf = new \ReflectionFunction('\\strlen'); print "--- getShortName(\"strlen\") ---\n"; var_dump($rf->getShortName()); print "\n"; print "--- getNamespaceName(\"strlen\") ---\n"; var_dump($rf->getNamespaceName()); print "\n";