ReflectionProperty_getValue_error.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. --TEST--
  2. Test ReflectionProperty::getValue() errors.
  3. --FILE--
  4. <?php
  5. class TestClass {
  6. public $pub;
  7. public $pub2 = 5;
  8. static public $stat = "static property";
  9. protected $prot = 4;
  10. private $priv = "keepOut";
  11. }
  12. class AnotherClass {
  13. }
  14. $instance = new TestClass();
  15. $instanceWithNoProperties = new AnotherClass();
  16. $propInfo = new ReflectionProperty('TestClass', 'pub2');
  17. echo "Too few args:\n";
  18. var_dump($propInfo->getValue());
  19. echo "\nToo many args:\n";
  20. var_dump($propInfo->getValue($instance, true));
  21. echo "\nWrong type of arg:\n";
  22. var_dump($propInfo->getValue(true));
  23. echo "\nInstance without property:\n";
  24. $propInfo = new ReflectionProperty('TestClass', 'stat');
  25. echo "\nStatic property / too many args:\n";
  26. var_dump($propInfo->getValue($instance, true));
  27. echo "\nStatic property / wrong type of arg:\n";
  28. var_dump($propInfo->getValue(true));
  29. echo "\nProtected property:\n";
  30. try {
  31. $propInfo = new ReflectionProperty('TestClass', 'prot');
  32. var_dump($propInfo->getValue($instance));
  33. }
  34. catch(Exception $exc) {
  35. echo $exc->getMessage();
  36. }
  37. echo "\n\nInstance without property:\n";
  38. $propInfo = new ReflectionProperty('TestClass', 'pub2');
  39. var_dump($propInfo->getValue($instanceWithNoProperties));
  40. ?>
  41. --EXPECTF--
  42. Too few args:
  43. Warning: ReflectionProperty::getValue() expects exactly 1 parameter, 0 given in %s on line %d
  44. NULL
  45. Too many args:
  46. Warning: ReflectionProperty::getValue() expects exactly 1 parameter, 2 given in %s on line %d
  47. NULL
  48. Wrong type of arg:
  49. Warning: ReflectionProperty::getValue() expects parameter 1 to be object, boolean given in %s on line %d
  50. NULL
  51. Instance without property:
  52. Static property / too many args:
  53. string(15) "static property"
  54. Static property / wrong type of arg:
  55. string(15) "static property"
  56. Protected property:
  57. Cannot access non-public member TestClass::prot
  58. Instance without property:
  59. NULL