array_access_001.phpt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. --TEST--
  2. Test V8::executeString() : Check ArrayAccess live binding
  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. $this->data[$offset] = $value;
  20. }
  21. public function offsetUnset($offset) {
  22. throw new Exception('Not implemented');
  23. }
  24. public function count() {
  25. return count($this->data);
  26. }
  27. public function push($value) {
  28. $this->data[] = $value;
  29. }
  30. }
  31. } else {
  32. class MyArray implements ArrayAccess, Countable {
  33. private $data = Array('one', 'two', 'three');
  34. public function offsetExists($offset): bool {
  35. return isset($this->data[$offset]);
  36. }
  37. public function offsetGet(mixed $offset): mixed {
  38. return $this->data[$offset];
  39. }
  40. public function offsetSet(mixed $offset, mixed $value): void {
  41. $this->data[$offset] = $value;
  42. }
  43. public function offsetUnset(mixed $offset): void {
  44. throw new Exception('Not implemented');
  45. }
  46. public function count(): int {
  47. return count($this->data);
  48. }
  49. public function push($value) {
  50. $this->data[] = $value;
  51. }
  52. }
  53. }
  54. $v8 = new V8Js();
  55. $v8->myarr = new MyArray();
  56. $v8->executeString('var_dump(PHP.myarr.join(","));');
  57. /* array is "live bound", i.e. new elements just pop up on js side. */
  58. $v8->myarr->push('new');
  59. $v8->executeString('var_dump(PHP.myarr.join(","));');
  60. ?>
  61. ===EOF===
  62. --EXPECT--
  63. string(13) "one,two,three"
  64. string(17) "one,two,three,new"
  65. ===EOF===