test_call.php 2.0 KB

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