Hudzilla.org - the homepage of Paul Hudson
Contents > Java and COM > Bringing Java into the mix Wish List | Report Bug | About Me ]

14.2.1     Basic Java use

This is NOT the latest copy of this book; click here for the latest version.

As with COM, Java objects are encapsulated with a single class, "Java". This takes the name of the Java object to create as its constructor parameter, and returns the object. This is best illustrated with a very simple example, so here you go:

<?php
    $java
= new Java('java.lang.System');
    
$version = $java->getproperty('java.version');
    print
"Hello from Java $version!\n";
?>

Line one creates a new Java object of type java.lang.System. If you have used Java before, you will know that java.lang.System is a core class that offers some of the most basic functionality in Java. That line of code creates an instance of java.lang.System, and stores it in the $java PHP variable, upon which we can call functions, read variables, etc.

Line two demonstrates calling a function on the Java object, getProperty(). The getProperty() function is documented (at the time of writing) at http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getProperty(java.lang.String), and takes the string parameter of the property you want to read.

It is important to note that in Java, this function is called getProperty(), but thanks to PHP not having case-sensitive function names, you can use getproperty(). This might cause some confusion in poorly written classes where overloaded function calls are decided based upon capitalisation of the function names, but otherwise PHP does a good job of picking out which overloaded function to use, if any.

It is also important to note that Java is a strongly typed language, whereas PHP is weakly typed. Strictly speaking, getProperty() takes a string as its parameter, whereas PHP does not distinguish between strings, integers, and so on. However, when interacting with Java, PHP will decide which function you want to call, then automatically convert the parameters you pass in so that they match what the Java function expects. If you encounter problems with this, you might want to try explicitly typecasting your PHP variables before using them in a function call.

Our getProperty() call will return the specified value, in this case the version number of the JVM that is being used. This is captured in the $version variable, then printed out to the screen - simple, really.





<< 14.2 Bringing Java into the mix   14.2.2 The drawbacks of basic Java use >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is zero plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow