soluble-japha-pjb62-compat
, (*1)
Introduction
*** Work in progress ***, (*2)
Historically the PHP/Java bridge client didn't
support namespaces., (*3)
Install this package if you have existing code relying on legacy php-java-bridge and
don't want to refactor to the newer implementation provided by soluble/japha., (*4)
Features
- Make soluble-japha compatible with the procedural phpjavabridge legacy api
Requirements
Installation
-
PHP installation (client), (*5)
Through composer., (*6)
$ composer require "soluble/japha-pjb62-compat"
Most modern frameworks will include Composer out of the box, but ensure the following file is included:, (*7)
<?php
// include the Composer autoloader
require 'vendor/autoload.php';
-
PHP-Java-bridge server, (*8)
Refer to the latest documentation provided in the soluble-japha project., (*9)
Or as quick install guide use the standalone server :, (*10)
$ mkdir -p /my/path/pjbserver-tools
$ cd /my/path/pjbserver-tools
$ composer create-project --no-dev --prefer-dist "belgattitude/pjbserver-tools"
$ ./bin/pjbserver-tools pjbserver:start -vvv ./config/pjbserver.config.php.dist
The server will start on default port 8089. If you like to change it, create a local copy of ./config/pjbserver.config.php.dist
and refer it in the above command., (*11)
Use the commands pjbserver:stop, pjbserver:restart, pjbserver:status to control or query the server status., (*12)
For production the recommended way is to deploy the JavaBridge servlet into a J2EE compatible server (Tomcat,...).
Have a look to the complete java server installation documentation., (*13)
Examples
Connection example
Configure your bridge adapter with the correct driver (currently only Pjb62 is supported) and the PHP-Java-bridge server address., (*14)
<?php
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
$ba = new BridgeAdapter([
'driver' => 'Pjb62',
'servlet_address' => 'localhost:8089/servlet.phpjavabridge'
]);
This replace the include('xxx/Java.inc) used in previous versions., (*15)
Basic Java usage
With legacy mode enabled you can use the java* function directly, (*16)
<?php
use Soluble\Japha\Bridge\Adapter as BridgeAdapter;
$ba = new BridgeAdapter([
'driver' => 'Pjb62',
'servlet_address' => 'localhost:8083/servlet.phpjavabridge'
]);
$bigint = new Java("java.math.BigInteger", 1);
$system = java_class('java.lang.System);
java_instanceof($bigint, 'java.math.BigInteger'); // -> true
java_inspect($bigint);
java_values($bigint);
//java_invoke();
API
Refactor constants
| Constant |
Example |
JAVA_HOSTS |
define("JAVA_HOSTS", "127.0.0.1:8787") |
JAVA_SERVLET |
define("JAVA_SERVLET", "/MyWebApp/servlet.phpjavabridge") |
JAVA_PREFER_VALUES |
define("JAVA_PREFER_VALUES", 1) |
JAVA_LOG_LEVEL |
define("JAVA_LOG_LEVEL", null) |
JAVA_SEND_SIZE |
define("JAVA_SEND_SIZE", 8192) |
JAVA_RECV_SIZE |
define("JAVA_RECV_SIZE", 8192) |
JAVA_DISABLE_AUTOLOAD |
Not applicable anymore - PSR4 ;) |
Initialization
| Old way |
New way |
include(... /Java.inc) |
$ba = new Bridge\Adapter($option); |
API
The following table maps old and new recommended API., (*17)
| Legacy |
Bridge\Adapter ($ba) |
new Java($class, $args=null) : Java
|
$ba->java($class, $args=null) : Interfaces\JavaObject
|
java_class($class) : JavaClass
|
$ba->javaClass($class) Interfaces\JavaClass
|
java_instanceof($object, $class) : boolean
|
$ba->isInstanceOf($object, $class) : boolean
|
(under review, soon to be implemented), (*18)
| Legacy |
Bridge\Adapter ($ba) |
java_values($object) : mixed
|
$ba->getValues($object) : mixed
|
java_invoke($object, $method, $args=null) : `mixed |
null|$ba->invokeMethod($object, $method, $args=null) : string\null
|
java_inspect($object) : string
|
$ba->debug()->inspect($object) : string
|
getLastException : Exception
|
$ba->debug()->getLastException() : Exception
|
clearLastException |
$ba->debug()->clearLastException() |
function java_is_null($value)
function java_is_true($value)
function java_is_false($value), (*19)
Refactoring guidelines
Keep a step by step approach... you can use both API at the same time., (*20)
- Try to change intialization sequence
Coding standards