array_access_005.phpt 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --TEST--
  2. Test V8::executeString() : Export __invoke method 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. return count($this->data);
  27. }
  28. public function __invoke() {
  29. echo "__invoke called!\n";
  30. }
  31. }
  32. } else {
  33. class MyArray implements ArrayAccess, Countable {
  34. private $data = Array('one', 'two', 'three');
  35. public function offsetExists($offset): bool {
  36. return isset($this->data[$offset]);
  37. }
  38. public function offsetGet(mixed $offset): mixed {
  39. return $this->data[$offset];
  40. }
  41. public function offsetSet(mixed $offset, mixed $value): void {
  42. echo "set[$offset] = $value\n";
  43. $this->data[$offset] = $value;
  44. }
  45. public function offsetUnset(mixed $offset): void {
  46. throw new Exception('Not implemented');
  47. }
  48. public function count(): int {
  49. return count($this->data);
  50. }
  51. public function __invoke() {
  52. echo "__invoke called!\n";
  53. }
  54. }
  55. }
  56. $v8 = new V8Js();
  57. $v8->myarr = new MyArray();
  58. $v8->executeString('PHP.myarr();');
  59. $v8->executeString('var_dump(PHP.myarr.length);');
  60. $v8->executeString('var_dump(PHP.myarr.join(", "));');
  61. ?>
  62. ===EOF===
  63. --EXPECT--
  64. __invoke called!
  65. int(3)
  66. string(15) "one, two, three"
  67. ===EOF===