When you have a ReflectionParameter
, you can determine types in the following ways:
class MyClass
{
/**
* @param array $myParameter
*/
public function myMethod(array $myParameter = [])
{
// ... stuff ...
}
}
<?php
use Roave\BetterReflection\BetterReflection;
$classInfo = (new BetterReflection())->classReflector()->reflect('MyClass');
$methodInfo = $classInfo->getMethod('myMethod');
$parameterInfo = $methodInfo->getParameter('myParameter');
// Will fetch the language hint
var_dump($parameterInfo->getType());
// Will fetch an array of Type objects for the typehint in the DocBlock
var_dump($parameterInfo->getDocBlockTypes());
// Will fetch an array of strings describing the DocBlock type hints
var_dump($parameterInfo->getDocBlockTypeStrings());
This is compatible with the PHP 7 reflection API, and will return a \Roave\BetterReflection\Reflection\ReflectionType
instance.
This is compatible with the PHP 7 reflection API, and will return a \Roave\BetterReflection\Reflection\ReflectionType
instance.
The getDocBlockTypes()
method will return an array of type hints that are extracted from the DocBlock. These are read
by a phpDocumentor component, and so this returns an array of Type
objects (see below).
This is a convenience wrapper to fetch the types named as strings.
The Type
objects are actually types provided by the phpdocumentor/type-resolver
library, which provides different
types for describing PHP's internal types as well as classes, callables and so on. For more information, head over to
phpDocumentor/TypeResolver for this excellent library!