__set__get_001.phpt 1.0 KB

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