array_access_003.phpt 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. --TEST--
  2. Test V8::executeString() : Export PHP methods on ArrayAccess objects
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --INI--
  6. v8js.use_array_access = 1
  7. --FILE--
  8. <?php
  9. if (PHP_VERSION_ID < 80000) {
  10. class MyArray implements ArrayAccess, Countable {
  11. private $data = Array('one', 'two', 'three');
  12. public function offsetExists($offset) {
  13. return isset($this->data[$offset]);
  14. }
  15. public function offsetGet($offset) {
  16. return $this->data[$offset];
  17. }
  18. public function offsetSet($offset, $value) {
  19. echo "set[$offset] = $value\n";
  20. $this->data[$offset] = $value;
  21. }
  22. public function offsetUnset($offset) {
  23. throw new Exception('Not implemented');
  24. }
  25. public function count() {
  26. echo 'count() = ', count($this->data), "\n";
  27. return count($this->data);
  28. }
  29. public function phpSidePush($value) {
  30. echo "push << $value\n";
  31. $this->data[] = $value;
  32. }
  33. public function push($value) {
  34. echo "php-side-push << $value\n";
  35. $this->data[] = $value;
  36. }
  37. }
  38. } else {
  39. class MyArray implements ArrayAccess, Countable {
  40. private $data = Array('one', 'two', 'three');
  41. public function offsetExists($offset): bool {
  42. return isset($this->data[$offset]);
  43. }
  44. public function offsetGet(mixed $offset): mixed {
  45. return $this->data[$offset];
  46. }
  47. public function offsetSet(mixed $offset, mixed $value): void {
  48. echo "set[$offset] = $value\n";
  49. $this->data[$offset] = $value;
  50. }
  51. public function offsetUnset(mixed $offset): void {
  52. throw new Exception('Not implemented');
  53. }
  54. public function count(): int {
  55. echo 'count() = ', count($this->data), "\n";
  56. return count($this->data);
  57. }
  58. public function phpSidePush($value) {
  59. echo "push << $value\n";
  60. $this->data[] = $value;
  61. }
  62. public function push($value) {
  63. echo "php-side-push << $value\n";
  64. $this->data[] = $value;
  65. }
  66. }
  67. }
  68. $v8 = new V8Js();
  69. $v8->myarr = new MyArray();
  70. /* Call PHP method to modify the array. */
  71. $v8->executeString('PHP.myarr.phpSidePush(23);');
  72. var_dump(count($v8->myarr));
  73. var_dump($v8->myarr[3]);
  74. /* And JS should see the changes due to live binding. */
  75. $v8->executeString('var_dump(PHP.myarr.join(","));');
  76. /* Call `push' method, this should trigger the PHP method. */
  77. $v8->executeString('PHP.myarr.push(42);');
  78. var_dump(count($v8->myarr));
  79. var_dump($v8->myarr[4]);
  80. /* And JS should see the changes due to live binding. */
  81. $v8->executeString('var_dump(PHP.myarr.join(","));');
  82. ?>
  83. ===EOF===
  84. --EXPECT--
  85. push << 23
  86. count() = 4
  87. int(4)
  88. int(23)
  89. count() = 4
  90. string(16) "one,two,three,23"
  91. php-side-push << 42
  92. count() = 5
  93. int(5)
  94. int(42)
  95. count() = 5
  96. string(19) "one,two,three,23,42"
  97. ===EOF===