<?php
class Thing {
public function doStuffWithInteger( integer $number ) : integer {
return $number * $number ;
}
public function doStuffWithInt( int $number ) : int {
return $number * $number ;
}
}
//Instantiate the object
$thing = new Thing( ) ;
//Instantiate the reflection object
$reflector = new ReflectionClass( 'Thing' ) ;
//Now get all the methods from class Thing in to $methods array
$methods = $reflector -> getMethods ( ) ;
//Now go through the $methods array and get information about each parameter.
foreach ( $methods as $method )
{
$parameterOne = $method -> getParameters ( ) [ 0 ] ;
$parameterOneType = $parameterOne -> getType ( ) ; printf ( "- The \" %s \" parameter has the type \" %s \" .\n " , $parameterOne -> name , $parameterOneType -> __toString
( ) ) ;
if ( $parameterOneType -> isBuiltin ( ) ) {
printf ( " - \" %s \" is a builtin type.\n " , $parameterOneType -> __toString
( ) ) ; } else {
printf ( " - \" %s \" is not a builtin type.\n " , $parameterOneType -> __toString
( ) ) ; printf ( " - The \" %s \" class does exist.\n " , $parameterOneType -> __toString
( ) ) ; } else {
printf ( " - The \" %s \" class does not exist.\n " , $parameterOneType -> __toString
( ) ) ; }
}
}
printf ( "The return value of \$ thing->doStuffWithInt(4) is %s .\n " , $thing -> doStuffWithInt ( 4 ) ) ; printf ( "The return value of \$ thing->doStuffWithInteger(4) is %s .\n " , $thing -> doStuffWithInteger ( 4 ) ) ;
PD9waHAKCgpjbGFzcyBUaGluZyB7CglwdWJsaWMgZnVuY3Rpb24gZG9TdHVmZldpdGhJbnRlZ2VyKGludGVnZXIgJG51bWJlcik6IGludGVnZXIgewoJCXJldHVybiAkbnVtYmVyICogJG51bWJlcjsKCX0KCQoJcHVibGljIGZ1bmN0aW9uIGRvU3R1ZmZXaXRoSW50KGludCAkbnVtYmVyKTogaW50IHsKCQlyZXR1cm4gJG51bWJlciAqICRudW1iZXI7Cgl9Cn0KCi8vSW5zdGFudGlhdGUgdGhlIG9iamVjdAokdGhpbmcgPSBuZXcgVGhpbmcoKTsKCi8vSW5zdGFudGlhdGUgdGhlIHJlZmxlY3Rpb24gb2JqZWN0CiRyZWZs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+X190b1N0cmluZygpKTsKCQl9IGVsc2UgewoJCQlwcmludGYoIiAgICAtIFRoZSBcIiVzXCIgY2xhc3MgZG9lcyBub3QgZXhpc3QuXG4iLCAkcGFyYW1ldGVyT25lVHlwZS0+X190b1N0cmluZygpKTsKCQl9Cgl9Cn0KCnByaW50ZigiVGhlIHJldHVybiB2YWx1ZSBvZiBcJHRoaW5nLT5kb1N0dWZmV2l0aEludCg0KSBpcyAlcy5cbiIsICR0aGluZy0+ZG9TdHVmZldpdGhJbnQoNCkpOwpwcmludGYoIlRoZSByZXR1cm4gdmFsdWUgb2YgXCR0aGluZy0+ZG9TdHVmZldpdGhJbnRlZ2VyKDQpIGlzICVzLlxuIiwgJHRoaW5nLT5kb1N0dWZmV2l0aEludGVnZXIoNCkpOwo=
stdout
- The "number" parameter has the type "integer".
- "integer" is not a builtin type.
- The "integer" class does not exist.
- The "number" parameter has the type "int".
- "int" is a builtin type.
The return value of $thing->doStuffWithInt(4) is 16.
stderr
PHP Fatal error: Uncaught TypeError: Argument 1 passed to Thing::doStuffWithInteger() must be an instance of integer, integer given, called in /home/IMaj9A/prog.php on line 43 and defined in /home/IMaj9A/prog.php:5
Stack trace:
#0 /home/IMaj9A/prog.php(43): Thing->doStuffWithInteger(4)
#1 {main}
thrown in /home/IMaj9A/prog.php on line 5