|
From: <php...@li...> - 2010-04-28 03:25:32
|
Hi,
PHP/Java Bridge 6 does support PHP 4 and other languages such as Scheme
or even Python.
A simple Java class for PHP 4 is:
<?php
// The following is the "Java" class definition, stripped down to fit
// into one line. To use this sample start Java with: java -jar
// JavaBridge.jar INET:9267. Then type: php sample.php
//
class P{var$Pc="<C v=\"%s\" p=\"I\">",$PC="</C>",$Pi="<I v=\"%d\" m=\"%s
\" p=\"I\">",$PI="</I>",$Ps="<S v=\"%s\"/>",$Pl="<L v=\"%d\" p=\"%s
\"/>",$Po="<O v=\"%d\"/>",$c;function
str($s){fwrite($this->c,sprintf($this->Ps,$s));}function
obj($s){fwrite($this->c,sprintf($this->Po,$s->j));}function
P(){$this->c=fsockopen("127.0.0.1",9267);}function
cB($s){fwrite($this->c,sprintf($this->Pc,$s));}function
cE(){fwrite($this->c,$this->PC);}function iB($o,
$m){fwrite($this->c,sprintf($this->Pi,$o,$m));}function
iE(){fwrite($this->c,$this->PI);}function
v($s){if(is_object($s))$this->obj($s);else
$this->str((string)$s);}function res(){$r=sscanf(fread($this->c,8192),"%
s v=\"%[^\"]\"");return$r[1];}}function gP(){static$p;if(!$p)$p=new
P();return$p;}class Java{var$j,$p;function Java(){if(!
func_num_args())return;$this->p=gP();$ar=func_get_args();
$this->p->cB(array_shift($ar));foreach($ar as$arg)$this->p->v($arg);
$this->p->cE();$ar=sscanf($this->p->res(),"%d");$this->j=
$ar[0];}function __call($m,$a,&$rv){$this->p->iB($this->j,$m);foreach($a
as$arg)$this->p->v($arg);$this->p->iE();$p=new Java();
$ar=sscanf($this->p->res(),"%d");$p->j=$ar[0];$p->p=$this->p;$rv=
$p;return true;}function toString(){$this->p->iB("","castToString");
$this->p->v($this);$this->p->iE();return
base64_decode($this->p->res());}}overload("Java");
// Test
$i1 = new Java("java.math.BigInteger", "1");
$i2 = new Java("java.math.BigInteger", "2");
$i3 = $i1->add($i2);
echo $i3->toString() . "\n";
?>
Please see our FAQ and the examples/clients folder from the
documentation download for details.
Regards,
Jost Bökemeier
|