array_access_004.phpt 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class MyArray implements ArrayAccess, Countable {
  10. private $data = Array('one', 'two', 'three');
  11. private $privFoo = 23;
  12. protected $protFoo = 23;
  13. public $pubFoo = 42;
  14. /* We can have a length property on the PHP object, but the length property
  15. * of the JS object will still call count() method. Anyways it should be
  16. * accessibly as $length. */
  17. public $length = 42;
  18. public function offsetExists($offset) {
  19. return isset($this->data[$offset]);
  20. }
  21. public function offsetGet($offset) {
  22. return $this->data[$offset];
  23. }
  24. public function offsetSet($offset, $value) {
  25. echo "set[$offset] = $value\n";
  26. $this->data[$offset] = $value;
  27. }
  28. public function offsetUnset($offset) {
  29. throw new Exception('Not implemented');
  30. }
  31. public function count() {
  32. return count($this->data);
  33. }
  34. }
  35. $v8 = new V8Js();
  36. $v8->myarr = new MyArray();
  37. $v8->executeString('var_dump(PHP.myarr.privFoo);');
  38. $v8->executeString('var_dump(PHP.myarr.protFoo);');
  39. $v8->executeString('var_dump(PHP.myarr.pubFoo);');
  40. /* This should call count(), i.e. return 3 */
  41. $v8->executeString('var_dump(PHP.myarr.length);');
  42. /* This should print the value of the $length property */
  43. $v8->executeString('var_dump(PHP.myarr.$length);');
  44. ?>
  45. ===EOF===
  46. --EXPECT--
  47. NULL
  48. NULL
  49. int(42)
  50. int(3)
  51. int(42)
  52. ===EOF===