property_recreate_private.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. --TEST--
  2. Unsetting and recreating private properties.
  3. --FILE--
  4. <?php
  5. class C {
  6. private $p = 'test';
  7. function unsetPrivate() {
  8. unset($this->p);
  9. }
  10. function setPrivate() {
  11. $this->p = 'changed';
  12. }
  13. }
  14. class D extends C {
  15. function setP() {
  16. $this->p = 'changed in D';
  17. }
  18. }
  19. echo "Unset and recreate a superclass's private property:\n";
  20. $d = new D;
  21. $d->unsetPrivate();
  22. $d->setPrivate();
  23. var_dump($d);
  24. echo "\nUnset superclass's private property, and recreate it as public in subclass:\n";
  25. $d = new D;
  26. $d->unsetPrivate();
  27. $d->setP();
  28. var_dump($d);
  29. echo "\nUnset superclass's private property, and recreate it as public at global scope:\n";
  30. $d = new D;
  31. $d->unsetPrivate();
  32. $d->p = 'this will create a public property';
  33. var_dump($d);
  34. echo "\n\nUnset and recreate a private property:\n";
  35. $c = new C;
  36. $c->unsetPrivate();
  37. $c->setPrivate();
  38. var_dump($c);
  39. echo "\nUnset a private property, and attempt to recreate at global scope (expecting failure):\n";
  40. $c = new C;
  41. $c->unsetPrivate();
  42. $c->p = 'this will fail';
  43. var_dump($c);
  44. ?>
  45. ===DONE===
  46. --EXPECTF--
  47. Unset and recreate a superclass's private property:
  48. object(D)#%d (1) {
  49. ["p":"C":private]=>
  50. string(7) "changed"
  51. }
  52. Unset superclass's private property, and recreate it as public in subclass:
  53. object(D)#%d (1) {
  54. ["p"]=>
  55. string(12) "changed in D"
  56. }
  57. Unset superclass's private property, and recreate it as public at global scope:
  58. object(D)#%d (1) {
  59. ["p"]=>
  60. string(34) "this will create a public property"
  61. }
  62. Unset and recreate a private property:
  63. object(C)#%d (1) {
  64. ["p":"C":private]=>
  65. string(7) "changed"
  66. }
  67. Unset a private property, and attempt to recreate at global scope (expecting failure):
  68. Fatal error: Uncaught Error: Cannot access private property C::$p in %s:46
  69. Stack trace:
  70. #0 {main}
  71. thrown in %s on line 46