PHP Reflection - Get Method Parameter Type As String
I think the only way is to export
and manipulate the result string:
$refParam = new ReflectionParameter(array('Foo', 'Bar'), 0);
$export = ReflectionParameter::export(
array(
$refParam->getDeclaringClass()->name,
$refParam->getDeclaringFunction()->name
),
$refParam->name,
true
);
$type = preg_replace('/.*?(\w+)\s+\$'.$refParam->name.'.*/', '\\1', $export);
echo $type;
How to get parameter type in reflected class method (PHP 5.x)?
It looks like similar question already added in
PHP Reflection - Get Method Parameter Type As String
I wrote my solution it works for all cases:
/**
* @param ReflectionParameter $parameter
* @return string|null
*/
function getParameterType(ReflectionParameter $parameter)
{
$export = ReflectionParameter::export(
array(
$parameter->getDeclaringClass()->name,
$parameter->getDeclaringFunction()->name
),
$parameter->name,
true
);
return preg_match('/[>] ([A-z]+) /', $export, $matches)
? $matches[1] : null;
}
Get type of parameter with Reflection
You can do that by doing:
$params = $methods[0]->getParameters();
$params[0]->getClass()->name;
You can only use getClass()->name
if that param is strong typed.
How to get the string name of the argument's type hint?
In PHP 7 and later, you can use ReflectionParameter.getType
.
Example #1 ReflectionParameter::getType() example
<?php
function someFunction(int $param, $param2) {}
$reflectionFunc = new ReflectionFunction('someFunction');
$reflectionParams = $reflectionFunc->getParameters();
$reflectionType1 = $reflectionParams[0]->getType();
$reflectionType2 = $reflectionParams[1]->getType();
echo $reflectionType1;
var_dump($reflectionType2);
The above example will output something similar to:
int
null
Get class method arguments type
You have to use the ReflectionMethod
class instead of ReflectionFunction
:
class Test {
public function Test1(int $number, string $str) { }
}
//get the information about a specific method.
$rm = new ReflectionMethod('Test', 'Test1');
//get all parameter names and parameter types of the method.
foreach ($rm->getParameters() as $parameter) {
echo 'Name: '.$parameter->getName().' - Type: '.$parameter->getType()."\n";
}
demo: https://ideone.com/uBUghi
You can use the following solution to get all parameters of all methods, using ReflectionClass
:
class Test {
public function Test1(int $number, string $str) { }
public function Test2(bool $boolean) { }
public function Test3($value) { }
}
//get the information of the class.
$rf = new ReflectionClass('Test');
//run through all methods.
foreach ($rf->getMethods() as $method) {
echo $method->name."\n";
//run through all parameters of the method.
foreach ($method->getParameters() as $parameter) {
echo "\t".'Name: '.$parameter->getName().' - Type: '.$parameter->getType()."\n";
}
}
demo: https://ideone.com/Ac7M2L
php: get variable type hint using reflection
Try:
<?php
class Expense {
/**
* @var int
*/
private $id;
}
$refClass = new ReflectionClass('Expense');
foreach ($refClass->getProperties() as $refProperty) {
if (preg_match('/@var\s+([^\s]+)/', $refProperty->getDocComment(), $matches)) {
list(, $type) = $matches;
var_dump($type);
}
}
Output:
string(3) "int"
Get method's arguments?
You can use reflection...
$r = new ReflectionMethod($className, $methodName);
$params = $r->getParameters();
foreach ($params as $param) {
//$param is an instance of ReflectionParameter
echo $param->getName();
echo $param->isOptional();
}
How do I get the type of constructor parameter via reflection?
Try this out.
class Foo {
public function __construct(Bar $test) {
}
}
class Bar {
public function __construct() {
}
}
$reflection = new ReflectionClass('Foo');
$params = $reflection->getConstructor()->getParameters();
foreach ($params AS $param) {
echo $param->getClass()->name . '<br>';
}
How to test if Callable parameter will return a string with Reflection?
Maybe you need something like this:
class MyClass
{
protected static $overrider = null;
public static function setOverrider(Callable $callback)
{
$reflection = new ReflectionFunction($callback);
if ('string' != $reflection->getReturnType()) {
throw new \Exception('Wasnt a string!');
}
self::$overrider = $callback;
}
}
So, as I mentioned previously in comments: You need to declare returning type of your callable (which is a PHP7+ feature). It is a MUST, otherwise, it will not work
Like this:
function my_function(): string
{
return 'hello';
}
or like this if you prefer anonymous functions (Closure):
$my_callable = function(): string {
return 'hello';
}
It is as simple as this:
The interpreter cannot know the returning data type of a function without invoking it if you don't first tell the interpreter what should return the function in question.
Related Topics
How to Search Array for Multiple Values in PHP
How to Login Using Github, Facebook, Gmail and Twitter in Laravel 5.1
Opposite of Nl2Br? Is It Str_Replace
Display Only 3 Foreach Result Per Row
What Does a Leading Zero Do for a PHP Int
How to Log in to PHPmyadmin with Wamp, What Is the Username and Password
When Is It Good to Use Pass by Reference in PHP
How to Read Xmp Data from a Jpg with PHP
Using Named Parameters with Pdo for Like
PHP - Add Two Hours to Date Variable
Php, Merging Arrays with Common Keys
How to Get Files in Ftp Folder Sorted by Modification Time
Get the Values of 2 HTML Input Tags Having the Same Name Using PHP