13.6 Accessing Predefined Java Methods and Classes


13.6 Accessing Predefined Java Methods and Classes

You want to access methods and classes from Java's set of built-in methods and classes (that is, java.io.lang).

Technique

No problem! As of PHP 4, Java support is built in:

 <?php $formatter = new Java("java.text.SimpleDateFormat",                       "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"); print $formatter->format(new Java("java.util.date")); ?> 

Comments

Here we create a special object, the Java object. The first argument to the constructor is the class we want to work with (in this case, it is java.text.SimpleDateFormat ), and the second argument (as well as the third, fourth, and so on) are the arguments we want to pass to the class specified in the first argument.

Having the ability to interface with Java is tremendously useful. For example, much of the XML and XSL functionality in the Open Source world is available only through Java classes. The PHP-Java connection enables you to harness this power for your applications. For example, you could generate XML in PHP and then pass it to a Java XSL formatter class to generate HTML.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net