array_access_006.phpt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. --TEST--
  2. Test V8::executeString() : Enumerate ArrayAccess keys
  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', null, 'five');
  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. return count($this->data);
  27. }
  28. }
  29. } else {
  30. class MyArray implements ArrayAccess, Countable {
  31. private $data = Array('one', 'two', 'three', null, 'five');
  32. public function offsetExists($offset): bool {
  33. return isset($this->data[$offset]);
  34. }
  35. public function offsetGet(mixed $offset): mixed {
  36. return $this->data[$offset];
  37. }
  38. public function offsetSet(mixed $offset, mixed $value): void {
  39. echo "set[$offset] = $value\n";
  40. $this->data[$offset] = $value;
  41. }
  42. public function offsetUnset(mixed $offset): void {
  43. throw new Exception('Not implemented');
  44. }
  45. public function count(): int {
  46. return count($this->data);
  47. }
  48. }
  49. }
  50. $v8 = new V8Js();
  51. $v8->myarr = new MyArray();
  52. $js = <<<EOF
  53. var jsarr = [ "one", "two", "three", , "five" ];
  54. for(var i in jsarr) {
  55. var_dump(i);
  56. }
  57. for(var i in PHP.myarr) {
  58. var_dump(i);
  59. }
  60. EOF;
  61. $v8->executeString($js);
  62. ?>
  63. ===EOF===
  64. --EXPECT--
  65. string(1) "0"
  66. string(1) "1"
  67. string(1) "2"
  68. string(1) "4"
  69. string(1) "0"
  70. string(1) "1"
  71. string(1) "2"
  72. string(1) "4"
  73. ===EOF===