fork download
  1. <?php
  2.  
  3. class JSObject implements ArrayAccess {
  4. private $data = array();
  5.  
  6. function __construct($d) {
  7. $this->data = array_replace($this->data, $d);
  8. }
  9.  
  10. public function __set($name, $value) {
  11. $this->data[$name] = $value;
  12. }
  13. public function __get($name) {
  14. global $undefined;
  15. if(array_key_exists($name, $this->data)) {
  16. return $this->data[$name];
  17. } else {
  18. return $undefined;
  19. }
  20. }
  21. public function __isset($name) {
  22. return array_key_exists($name, $this->data);
  23. }
  24. public function __unset($name) {
  25. if(array_key_exists($name, $this->data)) {
  26. unset($this->data[$name]);
  27. }
  28. }
  29.  
  30. public function offsetExists($item) {
  31. return $this->__isset($item);
  32. }
  33. public function offsetGet($item) {
  34. return $this->__get($item);
  35. }
  36. public function offsetSet($item, $value) {
  37. $this->__set($item, $value);
  38. }
  39. public function offsetUnset($item) {
  40. $this->__unset($item);
  41. }
  42.  
  43. function __toString() {
  44. return '[object Object]';
  45. }
  46.  
  47. public function __debugInfo() {
  48. return $this->data;
  49. }
  50. }
  51.  
  52. class JSUndefined implements ArrayAccess {
  53. public function __set($name, $value) {
  54. global $undefined;
  55. return $undefined;
  56. }
  57. public function __get($name) {
  58. global $undefined;
  59. return $undefined;
  60. }
  61. public function __isset($name) {
  62. global $undefined;
  63. return $undefined;
  64. }
  65. public function __unset($name) {
  66. global $undefined;
  67. return $undefined;
  68. }
  69.  
  70. public function offsetExists($item) {
  71. global $undefined;
  72. return $undefined;
  73. }
  74. public function offsetGet($item) {
  75. global $undefined;
  76. return $undefined;
  77. }
  78. public function offsetSet($item, $value) {
  79. global $undefined;
  80. return $undefined;
  81. }
  82. public function offsetUnset($item) {
  83. global $undefined;
  84. return $undefined;
  85. }
  86.  
  87. function __toString() {
  88. return 'undefined';
  89. }
  90. }
  91.  
  92. $undefined = new JSUndefined();
  93.  
  94.  
  95. $x = new JSObject(['a' => 12]);
  96. echo sprintf("%s | %s\n", $x, print_r($x, true));
  97.  
  98. echo sprintf("(%s, %s)\n", $x->a, $x['a']);
  99.  
  100. $x->a = 42;
  101.  
  102. echo sprintf("(%s, %s)\n", $x->a, $x['a']);
  103.  
  104. $x['b'] = 43;
  105.  
  106. echo sprintf("(%s, %s)\n", $x->b, $x['b']);
  107.  
  108. unset($x->b);
  109.  
  110. echo sprintf("%s\n", $x->b);
  111.  
  112. echo sprintf("%s\n", var_export($x->b == $undefined, true));
  113.  
  114. echo sprintf("%s\n", print_r($x, true));
  115.  
Success #stdin #stdout 0s 82560KB
stdin
Standard input is empty
stdout
[object Object] | JSObject Object
(
    [a] => 12
)

(12, 12)
(42, 42)
(43, 43)
undefined
true
JSObject Object
(
    [a] => 42
)