From: <php...@li...> - 2011-01-28 16:47:32
|
Hi again Jost, That seems to be the clue I was needing. Since we can have a very high number of classes, I thought about adding methods to a php factory class so we can call methods and read properties of any php class without having to create an interface for each one (we'll still create interfaces for the most important classes only). Here's our class: class PHPClassFactory { private static $_instance; public static function getInstance() { if (! self::$_instance) self::$_instance = new self(); return self::$_instance; } public static function getClosure() { return java_closure( PHPClassFactory::getInstance(), null, java('Testing$PhpFactory')); } public function makeObject($tObj, $arObj) { $name = java_values($tObj); $ar = java_values($arObj); $obj = new $name($ar); return java_closure($obj); } public function call($j_instance, $j_method, $j_params) { $instance = java_unwrap( $j_instance ); $method = java_values( $j_method ); $params = array(); foreach ($j_params as $j_param) { $params[] = java_values($j_param); } $return = call_user_func_array(array($instance, $method), $params); $j_instance = java_closure($instance); if (is_object($valor)) { return java_closure($return); } else { return $return; } } public function set($j_instance, $j_property, $j_val) { $instance = java_unwrap( $j_instance ); $property = java_values( $j_property ); $instance->$property = java_values( $j_val ); $j_instance = java_closure($instance); } public function get($j_instance, $j_property) { $instance = java_unwrap( $j_instance ); $property = java_values( $j_property ); $valor = $instance->$property; if (is_object($valor)) { return java_closure($valor); } else { return $valor; } } } So, in java we can use code like this: public static interface PhpFactory { Object makeObject(String clase, Object[] params); Object call(Object inst, String method, Object[] params); Object set(Object inst, String method, Object value); Object get(Object inst, String property); } // ... Object record = factory.makeObject("rhcmsTestTable", new Object[] {}); factory.set(record, "name", "Alfalfa"); factory.call(record, "save", new Object[] {}); // Would be equivalent to: // $record = new rhcmsTestTable(); // $record->name = "Alfalfa"; // $record->save(); So far, all the test we have done (not a lot yet, tho) this code works as expected. What do you think about this approach? El 27/01/2011 21:18, php...@li... escribió: > Hi, > > your > > new $class() > > is wrong. $class is a JavaObject and new $JavaObject() requires at least > one argument. > > Furthermore I'd use interfaces rather than the invocation handler: > public class Test { > public static interface PhpObjectFactory { > PhpObject makeObject(String t, Object[] args); > } > public static interface PhpObject { > void doSomething(Object[] args); > int getResult(); > } > > public static int runTests(PhpObjectFactory factory) throws Exception { > PhpObject[] objects = new PhpObject[100]; > for(int i=0; i<4; i++) { > objects[i] = factory.makeObject("ClassTest"+i, new Object[]{i}); > } > > for (int i=0; i<4; i++) { > objects[i].doSomething(new Object[]{i}); > } > > int res=0; > for (int i=0; i<4; i++) { > res+=objects[i].getResult(); > } > return res; > } > } > > <?php require_once("http://localhost:8080/JavaBridge/java/JavaBridge.inc"); > > class ClassTest0 { > var $i; > function ClassTest0 ($ar) { > $this->i = $ar[0]; > } > function doSomething($ar) { > $this->i += java_values($ar[0]); > } > function getResult() { > echo "result:{$this->i}\n"; > return $this->i; > } > } > class ClassTest1 extends ClassTest0{}; > class ClassTest2 extends ClassTest1{}; > class ClassTest3 extends ClassTest2{}; > > function makeObject($tObj, $arObj) { > $name = java_values($tObj); > $ar = java_values($arObj); > $obj = new $name($ar); > return java_closure($obj, null, java('Test$PhpObject')); > } > > echo java("Test")->runTests(java_closure(null, null, > java('Test$PhpObjectFactory'))); > ?> > > javac Test.java > jar cf Test.jar *.class > java -Djava.ext.dirs=. -jar JavaBridge.jar > > php -n -dallow_url_include=On test.php > > => 12 > > > 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 -- *Jaume López * Área de Innovación jau...@la... <mailto:jau...@la...> http://www.lavinia.tc/logos/lavinia_logo.gif www.laviniainteractiva.com <http://www.laviniainteractiva.com/> www.vertice360.com <http://www.vertice360.com/> T(34) 93 972 34 10 - ext 226 http://www.lavinia.tc/logos/vertice.jpg |