test_call.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class Foo {
  3. var $bar = 'foobar';
  4. function MyOwnFunc() {}
  5. function __Get($name) {
  6. echo "Called __get(): ";
  7. var_dump($name);
  8. }
  9. function __Set($name, $args) {
  10. echo "Called __set(): ";
  11. var_dump($name, $args);
  12. }
  13. function __Isset($name) {
  14. echo "Called __isset(): ";
  15. var_dump($name);
  16. return true;
  17. }
  18. function __call($name, $args) {
  19. echo "Called __call(): ";
  20. var_dump($name, $args);
  21. }
  22. function __Invoke($name, $arg1, $arg2) {
  23. echo "Called __invoke(): ";
  24. var_dump(func_get_args());
  25. return 'foobar';
  26. }
  27. function __toString() {
  28. echo "Called __tostring: ";
  29. return $this->bar;
  30. }
  31. }
  32. $blaa = new V8Js();
  33. $blaa->obj = new Foo;
  34. try {
  35. echo "__invoke()\n";
  36. $blaa->executeString("var_dump(PHP.obj('arg1','arg2','arg3'));", "invoke_test1 #1.js");
  37. echo "------------\n";
  38. echo " __invoke() with new\n";
  39. $blaa->executeString("myobj = new PHP.obj('arg1','arg2','arg3'); var_dump(myobj);", "invoke_test2 #2.js");
  40. echo "------------\n";
  41. echo " __tostring()\n";
  42. $blaa->executeString('print(PHP.obj + "\n");', "tostring_test #3.js");
  43. echo "------------\n";
  44. echo " __isset() not called with existing property\n";
  45. $blaa->executeString('if ("bar" in PHP.obj) print("bar exists\n");', "isset_test1 #4.js");
  46. echo "------------\n";
  47. echo " __isset() with non-existing property\n";
  48. $blaa->executeString('if (!("foobar" in PHP.obj)) print("foobar does not exist\n"); else print("We called __isset and it said yes!\n");', "isset_test2 #5.js");
  49. echo "------------\n";
  50. echo " __get() not called with existing property\n";
  51. $blaa->executeString('var_dump(PHP.obj.bar);', "get_test1 #6.js");
  52. echo "------------\n";
  53. echo " __get() with non-existing property\n";
  54. $blaa->executeString('var_dump(PHP.obj.foo);', "get_test2 #7.js");
  55. echo "------------\n";
  56. echo " __call()\n";
  57. $blaa->executeString('PHP.obj.foo(1,2,3);', "call_test1 #8.js");
  58. echo "------------\n";
  59. } catch (V8JsScriptException $e) {
  60. echo $e->getMessage(), "\n";
  61. }