array_access_003.phpt 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }
  33. $v8 = new V8Js();
  34. $v8->myarr = new MyArray();
  35. /* Call PHP method to modify the array. */
  36. $v8->executeString('PHP.myarr.phpSidePush(23);');
  37. var_dump(count($v8->myarr));
  38. var_dump($v8->myarr[3]);
  39. /* And JS should see the changes due to live binding. */
  40. $v8->executeString('var_dump(PHP.myarr.join(","));');
  41. ?>
  42. ===EOF===
  43. --EXPECT--
  44. push << 23
  45. count() = 4
  46. int(4)
  47. int(23)
  48. count() = 4
  49. string(16) "one,two,three,23"
  50. ===EOF===