Basic usage
Пример #1 Java Example
<?php
// 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 />';
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
echo 'OS=' . $system->getProperty('os.name') . ' ' .
$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");
echo $formatter->format(new Java('java.util.Date'));
?>
Пример #2 AWT Example
<?php
// 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->add('North', $button);
$frame->validate();
$frame->pack();
$frame->visible = True;
$thread = new Java('java.lang.Thread');
$thread->sleep(10000);
$frame->dispose();
?>
- 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.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Другие службы
- PHP / Java Integration
Коментарии
Session sharing with php and java
------------------------------------------
<?php require_once("java/Java.inc");
$session = java_session();
?>
<HTML>
<TITLE>PHP and JSP session sharing</title>
<BODY>
<?php
if(is_null(java_values($session->get("counter")))) {
$session->put("counter", 1);
}
$counter = java_values($session->get("counter"));
print "HttpSession variable \"counter\": $counter<br>\n";
$session->put("counter", $counter+1);
?>
<a href="sessionSharing.jsp">JSP page</a>
</BODY>
</HTML>
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.
<?php
java_require("F:\\wamp5\\www\\classes\\");
$obj = new Java("hello");
// Call the "sayHello()" method
$output = $obj->SayHello();
echo $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.
@java_reset();
?>
That's it. Now browse to the PHP page and you should get
the results shown below.
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.