|
From: <php...@li...> - 2011-01-27 17:40:51
|
Hi Jost,
Thank you for the answer. I managed to pass closures of objects created
in the main php file to java as parameters of functions.
But since we have a large amount off objects in our project, the ideal
thing would be that the java code should be able to instantiate classes
defined in php by itself.
To do so, I created the following classes.
In php:
class PHPClassFactory
{
private static $_instance;
public static function getInstance()
{
if (! isset(self::$_instance)) self::$_instance = new self();
return self::$_instance;
}
public static function getClosure()
{
return java_closure(self::getInstance());
}
public static function instanciate($class, $params = null)
{
return java_closure( new $class() );
}
}
In java:
public class PHPClassFactory
{
private static Proxy factory;
private static PHPClassFactory instance;
public static void setProxy(Proxy phpproxy)
{
factory = phpproxy;
}
public static PHPClassFactory getInstance()
{
if (instance == null) instance = new PHPClassFactory();
return instance;
}
public static Proxy instanciate(String clase) throws Throwable
{
return instanciate(clase, null);
}
public static Proxy instanciate(String clase, Object[] params) throws
Throwable
{
PhpProcedure phpp = (PhpProcedure) Proxy.getInvocationHandler(factory);
return (Proxy) phpp.invoke(factory, "instanciate", new Object[] {
clase } );
}
public static Object call(Proxy object, String method, Object[]
params) throws Throwable
{
PhpProcedure phpp = (PhpProcedure) Proxy.getInvocationHandler(object);
return (Proxy) phpp.invoke(object, method, params);
}
}
So any java code (called from the main php) should be able to
instantiate a php class and use its methods like this:
PHPClassFactory.setProxy(factory);
Proxy foo = PHPClassFactory.instanciate("FooPHPClass");
return (String) PHPClassFactory.call(foo, "greet", null);
However, it doesn't work. Running that gives a warning and a fatal error
(which probably is because the previous warning):
Warning: Missing argument 1 for java_InternalJava::java_InternalJava()
and
Fatal error: Uncaught [[o:Exception]:"java.lang.Exception: Invoke
failed: [[o:helloworld]]->useGreetTwo((o:Proxy)[o:$Proxy0]). Cause:
java.lang.NoSuchMethodError: greet
I'm out of ideas right now. Can you give me a clue, please?
Thank you again,
Jaume Lopez
El 26/01/2011 22:25, php...@li... escribió:
> Hi,
>
> interesting concept. But it doesn't work, because the jsr223 api doesn't
> know about the existing php continuation and creates a new one.
>
> Jsr223 is for java->php->java... calls. For php->java->php... calls use
> java_closure:
>
> function toString() { return "i am a php Object method called from java
> called from php.";}
>
> echo java_closure();
>
> Please see java_closure API doc for details.
>
> Regards,
> Jost Bökemeier
> ------------------------------------------------------------------------------
> Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
> Finally, a world-class log management solution at an even better price-free!
> Download using promo code Free_Logger_4_Dev2Dev. Offer expires
> February 28th, so secure your free ArcSight Logger TODAY!
> http://p.sf.net/sfu/arcsight-sfd2d
> _______________________________________________
> php-java-bridge-users mailing list
> php...@li...
> https://lists.sourceforge.net/lists/listinfo/php-java-bridge-users
|