js-construct-protected-ctor.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. --TEST--
  2. Test V8::executeString() : Test PHP object construction controlled by JavaScript (protected ctor)
  3. --SKIPIF--
  4. <?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
  5. --FILE--
  6. <?php
  7. $v8 = new V8Js();
  8. class Greeter {
  9. protected $_name = null;
  10. protected function __construct($name) {
  11. echo "ctor called (php)\n";
  12. $this->_name = $name;
  13. }
  14. static function getInstance($name) {
  15. return new Greeter($name);
  16. }
  17. function sayHello() {
  18. echo "Hello ".$this->_name."\n";
  19. }
  20. }
  21. $v8->greeter = Greeter::getInstance("John");
  22. try {
  23. $v8->executeString('
  24. PHP.greeter.sayHello();
  25. var ngGreeter = new PHP.greeter.constructor("Ringo");
  26. ngGreeter.sayHello();
  27. ', 'ctor-test');
  28. } catch(V8JsScriptException $e) {
  29. echo "caught js exception\n";
  30. var_dump($e);
  31. }
  32. ?>
  33. ===EOF===
  34. --EXPECTF--
  35. ctor called (php)
  36. Hello John
  37. caught js exception
  38. object(V8JsScriptException)#3 (11) {
  39. ["message":protected]=>
  40. string(56) "ctor-test:4: Call to protected __construct() not allowed"
  41. ["string":"Exception":private]=>
  42. string(0) ""
  43. ["code":protected]=>
  44. int(0)
  45. ["file":protected]=>
  46. string(%d) "%s"
  47. ["line":protected]=>
  48. int(29)
  49. ["trace":"Exception":private]=>
  50. array(1) {
  51. [0]=>
  52. array(6) {
  53. ["file"]=>
  54. string(%d) "%s"
  55. ["line"]=>
  56. int(29)
  57. ["function"]=>
  58. string(13) "executeString"
  59. ["class"]=>
  60. string(4) "V8Js"
  61. ["type"]=>
  62. string(2) "->"
  63. ["args"]=>
  64. array(2) {
  65. [0]=>
  66. string(109) "
  67. PHP.greeter.sayHello();
  68. var ngGreeter = new PHP.greeter.constructor("Ringo");
  69. ngGreeter.sayHello();
  70. "
  71. [1]=>
  72. string(9) "ctor-test"
  73. }
  74. }
  75. }
  76. ["previous":"Exception":private]=>
  77. NULL
  78. ["JsFileName":protected]=>
  79. string(9) "ctor-test"
  80. ["JsLineNumber":protected]=>
  81. int(4)
  82. ["JsSourceLine":protected]=>
  83. string(55) " var ngGreeter = new PHP.greeter.constructor("Ringo");"
  84. ["JsTrace":protected]=>
  85. NULL
  86. }
  87. ===EOF===