wandu/反射

此包已被弃用且不再维护。未建议替代包。

反射扩展库。

v0.3.0 2017-04-16 17:21 UTC

This package is not auto-updated.

Last update: 2022-04-02 04:46:42 UTC


README

Latest Stable Version Latest Unstable Version Total Downloads License

Build Status Code Coverage Scrutinizer Code Quality

反射扩展库。

安装

$ composer require wandu/reflection

文档

ReflectionCallable

它支持6种可调用对象类型。(参考 Wani Blog, Callable)

  1. 函数名的字符串。
  2. 类和静态方法名的字符串。
  3. 类和静态方法名的数组。
  4. 对象和方法名的数组。
  5. 具有__invoke方法的类的对象。(参考 魔法方法 __invoke)
  6. Closure对象。

以及另外两个。

  1. __call魔法方法。
  2. __callStatic魔法方法。
namespace Wandu\Reflection;

use ReflectionFunctionAbstract;
use Reflector;

class ReflectionCallable extends ReflectionFunctionAbstract implements Reflector {

    /* Method */
    public __construct( callable $callee )
    public mixed __invoke( ...$parameters )
    public string getCallableName()
    public boolean isMagicMethod()
    public int getReflectionType()
    public ReflectionFunctionAbstract getRawReflection()
    
    /* Inherited methods */
    final private void ReflectionFunctionAbstract::__clone ( void )
    public ReflectionClass ReflectionFunctionAbstract::getClosureScopeClass ( void )
    public object ReflectionFunctionAbstract::getClosureThis ( void )
    public string ReflectionFunctionAbstract::getDocComment ( void )
    public int ReflectionFunctionAbstract::getEndLine ( void )
    public ReflectionExtension ReflectionFunctionAbstract::getExtension ( void )
    public string ReflectionFunctionAbstract::getExtensionName ( void )
    public string ReflectionFunctionAbstract::getFileName ( void )
    public string ReflectionFunctionAbstract::getName ( void )
    public string ReflectionFunctionAbstract::getNamespaceName ( void )
    public int ReflectionFunctionAbstract::getNumberOfParameters ( void )
    public int ReflectionFunctionAbstract::getNumberOfRequiredParameters ( void )
    public array ReflectionFunctionAbstract::getParameters ( void )
    public ReflectionType ReflectionFunctionAbstract::getReturnType ( void )
    public string ReflectionFunctionAbstract::getShortName ( void )
    public int ReflectionFunctionAbstract::getStartLine ( void )
    public array ReflectionFunctionAbstract::getStaticVariables ( void )
    public bool ReflectionFunctionAbstract::hasReturnType ( void )
    public bool ReflectionFunctionAbstract::inNamespace ( void )
    public bool ReflectionFunctionAbstract::isClosure ( void )
    public bool ReflectionFunctionAbstract::isDeprecated ( void )
    public bool ReflectionFunctionAbstract::isGenerator ( void )
    public bool ReflectionFunctionAbstract::isInternal ( void )
    public bool ReflectionFunctionAbstract::isUserDefined ( void )
    public bool ReflectionFunctionAbstract::isVariadic ( void )
    public bool ReflectionFunctionAbstract::returnsReference ( void )
    abstract public void ReflectionFunctionAbstract::__toString ( void )
}

(参考 ReflectionFunctionAbstract类)

示例。

use Wandu\Reflection\ReflectionCallable;

// 1. string of function's name.
$reflection = new ReflectionCallable('yourfunctionname'); // OK
$reflection = new ReflectionCallable('Your\OwnNamespace\yourfunctionname'); // with namespace also OK.

// 2. string of class and static method's name.
$reflection = new ReflectionCallable('Your\OwnNamespace\MyClass::callMyMethod'); // OK

// 3. array of class and static method's name.
$reflection = new ReflectionCallable(['Your\OwnNamespace\MyClass', 'callMyMethod']); // OK

// 4. array of object and method's name.
$reflection = new ReflectionCallable([new Your\OwnNamespace\MyClass(), 'callMyMethod']); // OK

// 5. object of class that has `__invoke` method.
$reflection = new ReflectionCallable(new Your\OwnNamespace\ClassWithInvoke()); // OK

// 6. object of `Closure`
$reflection = new ReflectionCallable(function ($param1, $param2) { /* do something */ });

$reflection->getNumberOfParameters(); // return 2

// 7. __call
$reflection = new ReflectionCallable([new Your\OwnNamespace\HasCallClass, 'anything']);

$reflection->getNumberOfParameters(); // always return 0
$reflection->getNumberOfRequiredParameters(); // always return 0
$reflection->getParameters(); // always return []
$reflection->getShortName(); // return 'anything'
$reflection->getName(); // return 'anything'

// 8. __callStatic
$reflection = new ReflectionCallable([Your\OwnNamespace\HasCallStaticClass::class, 'anything']);

$reflection->getNumberOfParameters(); // always return 0
$reflection->getNumberOfRequiredParameters(); // always return 0
$reflection->getParameters(); // always return []
$reflection->getShortName(); // return 'anything'
$reflection->getName(); // return 'anything'