ReflectionParameter_003.phpt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. --TEST--
  2. ReflectionParameter class - isOptional, isDefaultValueAvailable and getDefaultValue methods.
  3. --CREDITS--
  4. Robin Fernandes <robinf@php.net>
  5. Steve Seear <stevseea@php.net>
  6. --FILE--
  7. <?php
  8. class ReflectTestClass {
  9. public static function staticMethod($paramOne, $anotherParam = "bob",
  10. &$thirdParam = "jack", $arrayParam = array('one')) {
  11. echo "hello from test\n";
  12. echo "third is $thirdParam\n";
  13. return ++$theIncrement;
  14. }
  15. }
  16. $jane = "jane";
  17. ReflectTestClass::staticMethod("bob", "jack");
  18. $refMethod = new ReflectionMethod('ReflectTestClass', 'staticMethod');
  19. $refParameters = $refMethod->getParameters();
  20. echo "parameter names from staticMethod method:\n\n";
  21. foreach($refParameters as $parameter) {
  22. var_dump($parameter);
  23. if($parameter->isOptional()) {
  24. echo "this parameter is optional\n";
  25. } else {
  26. echo "this parameter is not optional\n";
  27. }
  28. if($parameter->isDefaultValueAvailable()) {
  29. echo "this parameter has a default value\n";
  30. } else {
  31. echo "this parameter has no default value\n";
  32. }
  33. /*
  34. $val = 0;
  35. try {
  36. $val = $parameter->getDefaultValue();
  37. var_dump($val);
  38. } catch (ReflectionException $e) {
  39. print $e->getMessage();
  40. echo "\n";
  41. }
  42. */
  43. echo "\n";
  44. }
  45. ?>
  46. --EXPECTF--
  47. hello from test
  48. third is jack
  49. Warning: Undefined variable $theIncrement in %s on line %d
  50. parameter names from staticMethod method:
  51. object(ReflectionParameter)#%d (1) {
  52. ["name"]=>
  53. string(8) "paramOne"
  54. }
  55. this parameter is not optional
  56. this parameter has no default value
  57. object(ReflectionParameter)#%d (1) {
  58. ["name"]=>
  59. string(12) "anotherParam"
  60. }
  61. this parameter is optional
  62. this parameter has a default value
  63. object(ReflectionParameter)#%d (1) {
  64. ["name"]=>
  65. string(10) "thirdParam"
  66. }
  67. this parameter is optional
  68. this parameter has a default value
  69. object(ReflectionParameter)#%d (1) {
  70. ["name"]=>
  71. string(10) "arrayParam"
  72. }
  73. this parameter is optional
  74. this parameter has a default value