raphhh/trex-reflection

为可调用对象和类型提供反射辅助工具

1.1.0 2020-06-25 12:21 UTC

This package is auto-updated.

Last update: 2024-08-25 22:41:20 UTC


README

Latest Stable Version Build Status Scrutinizer Quality Score Code Coverage Dependency Status Total Downloads Reference Status License

PHP 工具,用于反射可调用对象和类型。

安装

使用 composer 命令

$ composer require raphhh/trex-reflection

文档

CallableReflection

您可以使用 CallableReflection 检查和调用可调用对象,例如回调或 Closure 等。

所有类型的可调用对象

您可以知道给定的可调用对象是哪种类型。

Closure(闭包)
$reflect = new CallableReflection(function(){});
$reflect->isClosure(); //true
函数
$reflect = CallableReflection('in_array')
$reflect->isFunction(); //true
静态方法
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->isMethod(); //true
$reflect->isStaticMethod(); //true
实例方法
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->isMethod(); //true
$reflect->isInstanceMethod(); //true
被调用的对象
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->isInvokedObject(); //true

检索上下文

您可以检索可调用对象,例如对象或方法名称等。

Closure(闭包)
$reflect = new CallableReflection(function(){});
$reflect->getClosure(); //closure
函数
$reflect = new CallableReflection('in_array')
$reflect->getFunctionName(); //'in_array'
静态方法
$reflect = new CallableReflection('\DateTime::createFromFormat');
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getClassName(); //'DateTime'
$reflect->getMethodName(); //'createFromFormat'
实例方法
$reflect = new CallableReflection(array(new \DateTime(), 'modify'));
$reflect->getClassName(); //'DateTime'
$reflect->getObject(); //DateTime instance
$reflect->getMethodName(); //'modify'
被调用的对象
class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->getClassName(); //'Bar'
$reflect->getObject(); //Bar instance

调用可调用对象

您可以用相同的方式调用任何类型的可调用对象。

使用参数列表

此方法调用方法并将所有参数传递给它。

$reflect = new CallableReflection('in_array')
$reflect->invoke(1, [0, 1]); //true

使用参数数组

此方法允许将数组中的每个值映射到函数的每个参数。在需要使用 func_get_args() 动态传递参数时很有用。

$reflect = new CallableReflection('in_array')
$reflect->invoke([1, [0, 1]]); //true

使用参数映射

此方法允许将数组的键映射到函数参数的名称。因此,参数的顺序无关紧要。

$closure = function($arg1, $arg2){
    return [$arg1, $arg2];
}

$reflect = new CallableReflection($closure)
$reflect->invokeA(['arg2' => 'arg2', 'arg1' => 'arg1'])); //['arg1', 'arg2']

检索关联的反射类

检索可调用对象的 ReflectionFunctionAbstract

对于函数或闭包
$reflect = new CallableReflection('in_array');
$reflect->getReflector(); //ReflectionFunction
对于方法
$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getReflector(); //ReflectionMethod
对于类

注意,对于类,我们得到当前对象的 __invoke 方法的 ReflectionMethod,而不是 ReflectionClass

class Bar{
    function __invoke(){}
}

$reflect = new CallableReflection(new Bar());
$reflect->getReflector(); //ReflectionMethod

TypeReflection

对变量或函数类型的反射。

什么是类型?

类型是一个与特定值相关的字符串,该值与 PHP 类型 之一相关。

$typeReflection = new TypeReflection('int');
$typeReflection->getType(); //"int"
$typeReflection->getStandardizedType(); //"integer"

如果您正在处理变量,可以使用 TypeReflection::createFromVariable($variable)

$foo = new stdClass();
$typeReflection = TypeReflection::createFromVariable($foo);
$typeReflection->getType(); //"stdClass"
$typeReflection->getStandardizedType(); //"object"

注意,TypeReflection 对大小写不敏感。

标准化类型

TypeReflection 使用以下值标准化类型:

  • void
  • mixed
  • null
  • boolean
  • string
  • integer
  • float
  • number
  • scalar
  • array
  • object
  • resource
  • callable
  • 未知类型

有效任何类型

识别
$typeReflection = new TypeReflection('string');
$typeReflection->isValid(); //true
$typeReflection = new TypeReflection('foo');
$typeReflection->isValid(); //false
boolean
$typeReflection = new TypeReflection('bool');
$typeReflection->isBoolean(); //true
$typeReflection = new TypeReflection('boolean');
$typeReflection->isBoolean(); //true
string
$typeReflection = new TypeReflection('string');
$typeReflection->isString(); //true
integer
$typeReflection = new TypeReflection('int');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('integer');
$typeReflection->isInteger(); //true
$typeReflection = new TypeReflection('long');
$typeReflection->isInteger(); //true
float
$typeReflection = new TypeReflection('float');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('double');
$typeReflection->isFloat(); //true
$typeReflection = new TypeReflection('real');
$typeReflection->isFloat(); //true
number

任何整数或浮点数值。

scalar

任何布尔值、字符串或数值。

array
$typeReflection = new TypeReflection('array');
$typeReflection->isArray(); //true
$typeReflection = new TypeReflection('int[]');
$typeReflection->isArray(); //true
object
$typeReflection = new TypeReflection('object');
$typeReflection->isObject(); //true
$typeReflection = new TypeReflection('Datetime');
$typeReflection->isObject(); //true
resource
$typeReflection = new TypeReflection('resource');
$typeReflection->isResource(); //true
callable
$typeReflection = new TypeReflection('callable');
$typeReflection->isCallable(); //true
void
$typeReflection = new TypeReflection('void');
$typeReflection->isVoid(); //true
null
$typeReflection = new TypeReflection('null');
$typeReflection->isNull(); //true
mixed
$typeReflection = new TypeReflection('mixed');
$typeReflection->isMixed(); //true

检索标准表示

$typeReflection = new TypeReflection('bool');
$typeReflection->getStandardizedType(); //boolean
$typeReflection = new TypeReflection('int');
$typeReflection->getStandardizedType(); //integer
$typeReflection = new TypeReflection('real');
$typeReflection->getStandardizedType(); //float
$typeReflection = new TypeReflection('int[]');
$typeReflection->getStandardizedType(); //array
$typeReflection = new TypeReflection('Datetime');
$typeReflection->getStandardizedType(); //object