array_access_004.phpt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --TEST--
  2. Test V8::executeString() : Export PHP properties 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. private $privFoo = 23;
  13. protected $protFoo = 23;
  14. public $pubFoo = 42;
  15. /* We can have a length property on the PHP object, but the length property
  16. * of the JS object will still call count() method. Anyways it should be
  17. * accessibly as $length. */
  18. public $length = 42;
  19. public function offsetExists($offset) {
  20. return isset($this->data[$offset]);
  21. }
  22. public function offsetGet($offset) {
  23. return $this->data[$offset];
  24. }
  25. public function offsetSet($offset, $value) {
  26. echo "set[$offset] = $value\n";
  27. $this->data[$offset] = $value;
  28. }
  29. public function offsetUnset($offset) {
  30. throw new Exception('Not implemented');
  31. }
  32. public function count() {
  33. return count($this->data);
  34. }
  35. }
  36. } else {
  37. class MyArray implements ArrayAccess, Countable {
  38. private $data = Array('one', 'two', 'three');
  39. private $privFoo = 23;
  40. protected $protFoo = 23;
  41. public $pubFoo = 42;
  42. /* We can have a length property on the PHP object, but the length property
  43. * of the JS object will still call count() method. Anyways it should be
  44. * accessibly as $length. */
  45. public $length = 42;
  46. public function offsetExists($offset): bool {
  47. return isset($this->data[$offset]);
  48. }
  49. public function offsetGet(mixed $offset): mixed {
  50. return $this->data[$offset];
  51. }
  52. public function offsetSet(mixed $offset, mixed $value): void {
  53. echo "set[$offset] = $value\n";
  54. $this->data[$offset] = $value;
  55. }
  56. public function offsetUnset(mixed $offset): void {
  57. throw new Exception('Not implemented');
  58. }
  59. public function count(): int {
  60. return count($this->data);
  61. }
  62. }
  63. }
  64. $v8 = new V8Js();
  65. $v8->myarr = new MyArray();
  66. $v8->executeString('var_dump(PHP.myarr.privFoo);');
  67. $v8->executeString('var_dump(PHP.myarr.protFoo);');
  68. $v8->executeString('var_dump(PHP.myarr.pubFoo);');
  69. /* This should call count(), i.e. return 3 */
  70. $v8->executeString('var_dump(PHP.myarr.length);');
  71. /* This should print the value of the $length property */
  72. $v8->executeString('var_dump(PHP.myarr.$length);');
  73. ?>
  74. ===EOF===
  75. --EXPECT--
  76. NULL
  77. NULL
  78. int(42)
  79. int(3)
  80. int(42)
  81. ===EOF===