__set__get_001.phpt 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. --TEST--
  2. ZE2 __set() and __get()
  3. --FILE--
  4. <?php
  5. class setter {
  6. public $n;
  7. public $x = array('a' => 1, 'b' => 2, 'c' => 3);
  8. function __get($nm) {
  9. echo "Getting [$nm]\n";
  10. if (isset($this->x[$nm])) {
  11. $r = $this->x[$nm];
  12. echo "Returning: $r\n";
  13. return $r;
  14. }
  15. else {
  16. echo "Nothing!\n";
  17. }
  18. }
  19. function __set($nm, $val) {
  20. echo "Setting [$nm] to $val\n";
  21. if (isset($this->x[$nm])) {
  22. $this->x[$nm] = $val;
  23. echo "OK!\n";
  24. }
  25. else {
  26. echo "Not OK!\n";
  27. }
  28. }
  29. }
  30. $foo = new Setter();
  31. // this doesn't go through __set()... should it?
  32. $foo->n = 1;
  33. // the rest are fine...
  34. $foo->a = 100;
  35. $foo->a++;
  36. $foo->z++;
  37. var_dump($foo);
  38. ?>
  39. --EXPECTF--
  40. Setting [a] to 100
  41. OK!
  42. Getting [a]
  43. Returning: 100
  44. Setting [a] to 101
  45. OK!
  46. Getting [z]
  47. Nothing!
  48. Setting [z] to 1
  49. Not OK!
  50. object(setter)#%d (2) {
  51. ["n"]=>
  52. int(1)
  53. ["x"]=>
  54. array(3) {
  55. ["a"]=>
  56. int(101)
  57. ["b"]=>
  58. int(2)
  59. ["c"]=>
  60. int(3)
  61. }
  62. }