array_access_007.phpt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --TEST--
  2. Test V8::executeString() : Delete (unset) 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');
  12. public function offsetExists($offset) {
  13. return isset($this->data[$offset]);
  14. }
  15. public function offsetGet($offset) {
  16. if(!$this->offsetExists($offset)) {
  17. return null;
  18. }
  19. return $this->data[$offset];
  20. }
  21. public function offsetSet($offset, $value) {
  22. $this->data[$offset] = $value;
  23. }
  24. public function offsetUnset($offset) {
  25. unset($this->data[$offset]);
  26. }
  27. public function count() {
  28. return max(array_keys($this->data)) + 1;
  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. if(!$this->offsetExists($offset)) {
  39. return null;
  40. }
  41. return $this->data[$offset];
  42. }
  43. public function offsetSet(mixed $offset, mixed $value): void {
  44. $this->data[$offset] = $value;
  45. }
  46. public function offsetUnset(mixed $offset): void {
  47. unset($this->data[$offset]);
  48. }
  49. public function count(): int {
  50. return max(array_keys($this->data)) + 1;
  51. }
  52. }
  53. }
  54. $v8 = new V8Js();
  55. $v8->myarr = new MyArray();
  56. $js = <<<EOF
  57. var jsarr = [ "one", "two", "three" ];
  58. delete jsarr[1];
  59. var_dump(jsarr.join(","));
  60. delete PHP.myarr[1];
  61. var_dump(PHP.myarr.join(","));
  62. EOF;
  63. $v8->executeString($js);
  64. ?>
  65. ===EOF===
  66. --EXPECT--
  67. string(10) "one,,three"
  68. string(10) "one,,three"
  69. ===EOF===