PHP code example of raphhh / trex-reflection

1. Go to this page and download the library: Download raphhh/trex-reflection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

raphhh / trex-reflection example snippets


$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

$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

$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']

$reflect = new CallableReflection('in_array');
$reflect->getReflector(); //ReflectionFunction

$reflect = new CallableReflection(array('\DateTime', 'createFromFormat'));
$reflect->getReflector(); //ReflectionMethod


class Bar{
    function __invoke(){}
}

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

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

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

$typeReflection = new TypeReflection('string');
$typeReflection->isValid(); //true

$typeReflection = new TypeReflection('foo');
$typeReflection->isValid(); //false

$typeReflection = new TypeReflection('bool');
$typeReflection->isBoolean(); //true

$typeReflection = new TypeReflection('boolean');
$typeReflection->isBoolean(); //true

$typeReflection = new TypeReflection('string');
$typeReflection->isString(); //true

$typeReflection = new TypeReflection('int');
$typeReflection->isInteger(); //true

$typeReflection = new TypeReflection('integer');
$typeReflection->isInteger(); //true

$typeReflection = new TypeReflection('long');
$typeReflection->isInteger(); //true

$typeReflection = new TypeReflection('float');
$typeReflection->isFloat(); //true

$typeReflection = new TypeReflection('double');
$typeReflection->isFloat(); //true

$typeReflection = new TypeReflection('real');
$typeReflection->isFloat(); //true

$typeReflection = new TypeReflection('array');
$typeReflection->isArray(); //true

$typeReflection = new TypeReflection('int[]');
$typeReflection->isArray(); //true

$typeReflection = new TypeReflection('object');
$typeReflection->isObject(); //true

$typeReflection = new TypeReflection('Datetime');
$typeReflection->isObject(); //true

$typeReflection = new TypeReflection('resource');
$typeReflection->isResource(); //true

$typeReflection = new TypeReflection('callable');
$typeReflection->isCallable(); //true

$typeReflection = new TypeReflection('void');
$typeReflection->isVoid(); //true

$typeReflection = new TypeReflection('null');
$typeReflection->isNull(); //true

$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