我有一些类似的代码:
QuercusEngine engine = new QuercusEngine();
Value value = engine.execute("<?php return $obj->getName(); ?>");
System.out.println(value);
(有关详细信息,请参阅http://wiki.caucho.com/Quercus:_Command_Line_Interface_(CLI))
我想将$obj
设置为java实例。像这样:
SomeObject someObject = new SomeObject();
engine.setParam("obj", someObject);
显然这是一个简单的例子,但重点是我希望能够在php脚本中使用已经实例化的java类的实例。我怎么能这样做?
答案 0 :(得分:0)
我不认为这在任何地方都有记录,但在查看源代码后,查看QuercusEngine正在做什么,以及一点点试错,这就是它所需要的:
Path path = new StringPath("<?php return $obj->getName(); ?>");
QuercusContext quercusContext = new QuercusContext();
ReadStream reader = path.openRead();
QuercusProgram program = QuercusParser.parse(quercusContext, null, reader);
WriteStream out = new WriteStream(StdoutStream.create());
QuercusPage page = new InterpretedPage(program);
Env env = new Env(quercusContext, page, out, null, null);
SomeObject someObj = new SomeObject();
JavaClassDef classDef = env.getJavaClassDefinition(someObject.getClass());
env.setGlobalValue("obj", new JavaValue(env, someObject, classDef));
Value value = NullValue.NULL;
try {
value = program.execute(env);
}
catch (QuercusExitException e) {
}
out.flushBuffer();
out.free();
System.out.println(value);
希望这有助于某人。我在Quercus 4.0.25中对此进行了测试。