fork download
  1. <?php
  2.  
  3.  
  4. class Thing {
  5. public function doStuffWithInteger(integer $number): integer {
  6. return $number * $number;
  7. }
  8.  
  9. public function doStuffWithInt(int $number): int {
  10. return $number * $number;
  11. }
  12. }
  13.  
  14. //Instantiate the object
  15. $thing = new Thing();
  16.  
  17. //Instantiate the reflection object
  18. $reflector = new ReflectionClass('Thing');
  19.  
  20. //Now get all the methods from class Thing in to $methods array
  21. $methods = $reflector->getMethods();
  22.  
  23. //Now go through the $methods array and get information about each parameter.
  24. foreach($methods as $method)
  25. {
  26. $parameterOne = $method->getParameters()[0];
  27. $parameterOneType = $parameterOne->getType();
  28. printf("- The \"%s\" parameter has the type \"%s\".\n", $parameterOne->name, $parameterOneType->__toString());
  29.  
  30. if ($parameterOneType->isBuiltin()) {
  31. printf(" - \"%s\" is a builtin type.\n", $parameterOneType->__toString());
  32. } else {
  33. printf(" - \"%s\" is not a builtin type.\n", $parameterOneType->__toString());
  34. if (class_exists($parameterOneType->__toString())) {
  35. printf(" - The \"%s\" class does exist.\n", $parameterOneType->__toString());
  36. } else {
  37. printf(" - The \"%s\" class does not exist.\n", $parameterOneType->__toString());
  38. }
  39. }
  40. }
  41.  
  42. printf("The return value of \$thing->doStuffWithInt(4) is %s.\n", $thing->doStuffWithInt(4));
  43. printf("The return value of \$thing->doStuffWithInteger(4) is %s.\n", $thing->doStuffWithInteger(4));
  44.  
Runtime error #stdin #stdout #stderr 0s 82560KB
stdin
Standard input is empty
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