Примеры

Пример #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();
?>
Notes:
  • 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 functionallity 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 interchangably. 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.

Коментарии

Java - php class

Third-party classes are used within a PHP application,
 I'll create a simple Java class that calculates 
sales tax based upon a price and taxation rate 
input by the user.

You'll need to compile salesTax.java using 
the Java compiler before you can use this class 
within the PHP script

import java.util.*;
import java.text.*;

public class SalesTax {

public String SalesTax(double price, double salesTax) 
{

    double tax = price * salesTax;

    NumberFormat numberFormatter;

    numberFormatter = NumberFormat.getCurrencyInstance();

    String priceOut = numberFormatter.format(price);

    String taxOut = numberFormatter.format(tax);

    numberFormatter = NumberFormat.getPercentInstance();

    String salesTaxOut = 
numberFormatter.format(salesTax);

    String str = "A sales Tax of " + salesTaxOut +

                 " on " + priceOut + " equals " + taxOut + ".";

    return str;

    }

}

file named salesTax.java, you'll need to compile it. 

a new, compiled file named salesTax.class
 will be created.

This is the compiled code that will be called by 
PHP to perform the calculation based on the data
 input by the user via an HTML form.

salesTaxInterface.php

<?php

// Format the HTML form.

$salesTaxForm = <<<SalesTaxForm

<form action="SalesTaxInterface.php" method="post">

   Price (ex. 42.56):<br>

   <input type="text" name="price" size="15" maxlength="15" value=""><br>

   Sales Tax rate (ex. 0.06):<br>

   <input type="text" name="tax" size="15" maxlength="15" value=""><br>

   <input type="submit" name="submit" 
value="Calculate!">

   </form>

SalesTaxForm;

if (! isset(
$submit)) :

   echo 
$salesTaxForm;

else :

   
// Instantiate the SalesTax class.
   
$salesTax = new Java("SalesTax");

   
// Don't forget to typecast in order to

   // conform with the Java method specifications.

   
$price = (double) $price;
   
$tax = (double) $tax;

   print 
$salesTax->SalesTax($price$tax);

endif;

?>

Troubleshooting

Chances are you will encounter various 
minor problems when you first attempt to integrate 
Java and PHP functionality, particularly if you are a 
relative newcomer to the Java programming 
environment. Even if the Java code compiles 
correctly, you may still encounter problems, 
largely due to differences found between the 
two languages. 

two of these differences here:

Data types

PHP is a loosely-typed language, which means it is
 rather lenient on the way variables are used. 
On the contrary, Java is a strongly-typed language,
 which means that its policies for handling 
variables and data types are rather stringent. 
To illustrate the problem this difference poses, 
take a moment to again review the code.
 Notice that I had to typecast the $price and 
$tax variables before passing them to the 
SalesTax() method, because this 
method requires that both input parameters
 are of type "double." If this is not done, then
 input such as 24 for price would cause an
 error to occur.

Furthermore, if you are not adamant in 
ensuring the correct data types are passed 
to the Java methods, you may receive 
unexpected results, although it will not be 
outwardly apparent that an error has occurred.
 Therefore, be careful!

Error reporting

Errors occurring within a PHP script are reported
 in accordance with the level of error-reporting
 specified in the php.ini file. Because the 
Java code is called from within the PHP script, 
any errors that arise from the Java code are 
displayed as PHP errors. If you would like to
 prevent these errors from being displayed 
to the browser, simply prefix a @ symbol to
 the PHP command.
2009-06-10 13:18:47
http://php5.kiev.ua/manual/ru/java.examples.html

    Поддержать сайт на родительском проекте КГБ