PHP 7.1.0 Release Candidate 4 Released

Basic usage

Example #1 Java Example

// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');

// demonstrate property access
echo 'Java version=' $system->getProperty('java.version') . '<br />';
'Java vendor=' $system->getProperty('java.vendor') . '<br />';
'OS=' $system->getProperty('') . ' ' .
$system->getProperty('os.version') . ' on ' .
$system->getProperty('os.arch') . ' <br />';

// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");

$formatter->format(new Java('java.util.Date'));

Example #2 AWT Example

// This example is only intended to be run using the CLI.

$frame  = new Java('java.awt.Frame''PHP');
$button = new Java('java.awt.Button''Hello Java World!');

$frame->visible True;

$thread = new Java('java.lang.Thread');

  • new Java() will create an instance of a class if a suitable constructor is available. If no parameters are passed and the default constructor is useful as it provides access to classes like java.lang.System which expose most of their functionality through static methods.
  • Accessing a member of an instance will first look for bean properties then public fields. In other words, print $date.time will first attempt to be resolved as $date.getTime(), then as $date.time.
  • Both static and instance members can be accessed on an object with the same syntax. Furthermore, if the java object is of type java.lang.Class, then static members of the class (fields and methods) can be accessed.
  • Exceptions raised result in PHP warnings, and NULL results. The warnings may be eliminated by prefixing the method call with an "@" sign. The following APIs may be used to retrieve and reset the last error:

  • Overload resolution is in general a hard problem given the differences in types between the two languages. The PHP Java extension employs a simple, but fairly effective, metric for determining which overload is the best match. Additionally, method names in PHP are not case sensitive, potentially increasing the number of overloads to select from. Once a method is selected, the parameters are coerced if necessary, possibly with a loss of data (example: double precision floating point numbers will be converted to boolean).
  • In the tradition of PHP, arrays and hashtables may pretty much be used interchangeably. Note that hashtables in PHP may only be indexed by integers or strings; and that arrays of primitive types in Java can not be sparse. Also note that these constructs are passed by value, so may be expensive in terms of memory and time.

add a note add a note

User Contributed Notes 3 notes

anto dot justus at gmail dot com
7 years ago
Session sharing with php and java

<?php require_once("java/");
$session = java_session();

<TITLE>PHP and JSP session sharing</title>

if(is_null(java_values($session->get("counter")))) {
$session->put("counter", 1);

$counter = java_values($session->get("counter"));
"HttpSession variable \"counter\": $counter<br>\n";
$session->put("counter", $counter+1);
<a href="sessionSharing.jsp">JSP page</a>
anto dot justus at gmail dot com
7 years ago
php java 

The PHP code here is simple and straight forward. It is not
OOP. But you probably got enough of that while doing the
Java code.


$obj = new Java("hello");

// Call the "sayHello()" method

$output = $obj->SayHello();

$output.' this text in PHP

; // Displays (so this comes from the class!)

// Call the "SayNumber()" method

$output = $obj->SayNumber();

// Displays (so this comes from the class!)
echo $output.' is a lucky number';

//Because the JVM caches everything we want reset while
playing with the code. Otherwise the

// a cached version of the same class file will be used
rather than the new one

echo "
Resetting back-end to initial state\n"

// suppress the warning message from the use of reset.



That's it. Now browse to the PHP page and you should get
the results shown below.
sebastiansajie at yahoo dot com
2 years ago
Hi, I tried example #1 but keep getting a - Class 'Java' not found error. Do I need to point to the library that has the java class ? Or do I need to download the 'java.lang.System' jar file and store it in my webserver and point to it? How may this be done. Thank you for your answer.
To Top