array_access_003.phpt 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. --TEST--
  2. Test V8::executeString() : Export PHP methods 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. class MyArray implements ArrayAccess, Countable {
  10. private $data = Array('one', 'two', 'three');
  11. public function offsetExists($offset) {
  12. return isset($this->data[$offset]);
  13. }
  14. public function offsetGet($offset) {
  15. return $this->data[$offset];
  16. }
  17. public function offsetSet($offset, $value) {
  18. echo "set[$offset] = $value\n";
  19. $this->data[$offset] = $value;
  20. }
  21. public function offsetUnset($offset) {
  22. throw new Exception('Not implemented');
  23. }
  24. public function count() {
  25. echo 'count() = ', count($this->data), "\n";
  26. return count($this->data);
  27. }
  28. public function phpSidePush($value) {
  29. echo "push << $value\n";
  30. $this->data[] = $value;
  31. }
  32. public function push($value) {
  33. echo "php-side-push << $value\n";
  34. $this->data[] = $value;
  35. }
  36. }
  37. $v8 = new V8Js();
  38. $v8->myarr = new MyArray();
  39. /* Call PHP method to modify the array. */
  40. $v8->executeString('PHP.myarr.phpSidePush(23);');
  41. var_dump(count($v8->myarr));
  42. var_dump($v8->myarr[3]);
  43. /* And JS should see the changes due to live binding. */
  44. $v8->executeString('var_dump(PHP.myarr.join(","));');
  45. /* Call `push' method, this should trigger the PHP method. */
  46. $v8->executeString('PHP.myarr.push(42);');
  47. var_dump(count($v8->myarr));
  48. var_dump($v8->myarr[4]);
  49. /* And JS should see the changes due to live binding. */
  50. $v8->executeString('var_dump(PHP.myarr.join(","));');
  51. ?>
  52. ===EOF===
  53. --EXPECT--
  54. push << 23
  55. count() = 4
  56. int(4)
  57. int(23)
  58. count() = 4
  59. string(16) "one,two,three,23"
  60. php-side-push << 42
  61. count() = 5
  62. int(5)
  63. int(42)
  64. count() = 5
  65. string(19) "one,two,three,23,42"
  66. ===EOF===