property_recreate_protected.phpt 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. --TEST--
  2. Unsetting and recreating protected properties.
  3. --FILE--
  4. <?php
  5. class C {
  6. protected $p = 'test';
  7. function unsetProtected() {
  8. unset($this->p);
  9. }
  10. function setProtected() {
  11. $this->p = 'changed';
  12. }
  13. }
  14. class D extends C {
  15. function setP() {
  16. $this->p = 'changed in D';
  17. }
  18. }
  19. $d = new D;
  20. echo "Unset and recreate a protected property from property's declaring class scope:\n";
  21. $d->unsetProtected();
  22. $d->setProtected();
  23. var_dump($d);
  24. echo "\nUnset and recreate a protected property from subclass:\n";
  25. $d = new D;
  26. $d->unsetProtected();
  27. $d->setP();
  28. var_dump($d);
  29. echo "\nUnset a protected property, and attempt to recreate it outside of scope (expected failure):\n";
  30. $d->unsetProtected();
  31. $d->p = 'this will fail';
  32. var_dump($d);
  33. ?>
  34. --EXPECTF--
  35. Unset and recreate a protected property from property's declaring class scope:
  36. object(D)#%d (1) {
  37. ["p":protected]=>
  38. string(7) "changed"
  39. }
  40. Unset and recreate a protected property from subclass:
  41. object(D)#%d (1) {
  42. ["p":protected]=>
  43. string(12) "changed in D"
  44. }
  45. Unset a protected property, and attempt to recreate it outside of scope (expected failure):
  46. Fatal error: Uncaught Error: Cannot access protected property %s::$p in %s:32
  47. Stack trace:
  48. #0 {main}
  49. thrown in %s on line 32