From: <php...@li...> - 2006-11-20 18:42:33
|
Hi Verena, > Why did I capture the top-level environment? java_closure() without any arguments captures the top-level environment, creates and returns a Java proxy for it. Of cause you could use invocable.invokeMethod(engine.get(objectBinding),...) to call a method[1]. But if you don't have more than one PHP class per php file, this shouldn't be necessary; you can capture the object directly, which has the advantage that you don't need to keep a dictionary of additional symbol->object bindings. > thought with engine.get("obj") > I get the PHP object returned Yes. (you'll get a Java proxy for the PHP object). > and so the method > saySomething is invoked on > that object? Yes. > What is the correct syntax to invoke a method on > MyClass()? > > I tried this: > PhpScriptEngine engine = new PhpScriptEngine(); > String s = "<?php \n"; > s += "require_once 'MyClass.php'; \n"; > s += "java_context()->setAttribute( 'obj', > java_closure(new MyClass()), > 100 ); \n"; > s += "?>"; > engine.eval( new StringReader(s) ); > Invocable inv = (Invocable) engine; > inv.invokeMethod( engine.get("obj"), "saySomething", > new Object() ); The above script is not invocable. The last line of a invocable PHP script must contain: java_context()->call(java_closure()); > java.io.IOException: Bad file descriptor > at java.io.FileOutputStream.writeBytes(Native Well, PHP simply terminates after a PHP script is executed. To keep it, you must suspend it and pass control back to the Java continuation. The java_context()->call(kont) calls the Java continuation with the PHP continuation as its argument, so that the Java continuation can call back into the PHP continuation kont whenever necessary: kont.call(scriptEngine.getContinuation()); Of course the above PHP<->Java continuation passing style is hidden behind the JSR223 interfaces. But on the PHP side the java_context()->call() must exist, otherwise the PHP script cannot be called from a (potentially) remote Java script engine interface. > Yes, it sounds like, but actually I'm not sure how > to do it. This faces things > are about the web framework, aren't they? In practice, yes. > So how do > I transform this to a > J2SE app? Just create one public PHP class per PHP file. Example file my/foo.php, my/bar.php foo.php: class my_Foo { function toString() {return "foo";} } java_context()->call(java_closure(new myFoo()); bar.php: class my_Bar { function toString() {return "bar";} } java_context()->call(java_closure(new myBar()); Now you need a XML file which describes your PHP "beans", or simply hard-code the names in your code: ScriptEngineManager m = new ScriptEngineManager(); ScriptEngine foo = m.getEngineByName("php"); foo.eval(new FileReader("my/foo.php")); ScriptEngine bar = m.getEngineByName("php"); bar.eval(new FileReader("my/bar.php")); // now you can call methods from the foo and bar // PHP classes > No, it instanciates a JavaSript object which has one > method called hello. Interesting. I thought that unlike PHP, the JavaScript object hierarchy is not orthogonal to the Java object hierarchy. Regards, Jost Boekemeier [1] In the old JSR223 proposal the invokeMethod() and invokeProcedure() methods where called "invoke()". ___________________________________________________________ Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de |