You probably already know that PHP comes with extensions for awide variety of different technologies. But did you know that you couldhook PHP up to Java, and use Java classes and Java Beans from withinyour PHP scripts? If this is news to you, keep reading.
getCelsius() - get the current value of the Celsius property
getFahrenheit() - get the current value of the Fahrenheit property
setCelsius(num) - set the current value of the Celsius property to num
setFahrenheit(num) - set the current value of the Fahrenheit property to num
convertCelsiusToFahrenheit(value) - convert Celsius value to Fahrenheit
convertFahrenheitToCelsius(value) - convert Fahrenheit value to Celsius
And here's a PHP script which combines user input with the Temperature Bean to perform temperature conversion:
// data type conversion
$myClass = new Java("Temperature");
if($units == "celsius" && $temp != "")
// use the Celsius functions
// print result
echo $myClass->getCelsius()," <sup>o</sup> Celsius is
else if($units == "fahrenheit" && $temp != "")
// use the Fahrenheit functions
// print result
echo $myClass->getFahrenheit() , " <sup>o</sup>
Fahrenheit is ",
$myClass->convertFahrenheitToCelsius($myClass->getFahrenheit()) , "
echo "Please enter a valid temperature and scale";
<form action="<? echo $PHP_SELF; ?>" method="post">
<input type ="text" name="temp" size="4" maxlength="4">
<input type ="submit" name="submit" value="Convert">
This script consists of two parts: the form which allows the
user to select a temperature scale and enter a tempterature value, and the form processor which actually uses the Bean to perform the conversion and display the result.
Here's what the form looks like:
Once this form has been submitted, an object is instantiated from the Temperature class, and the information provided by the user is used to perform temperature conversion using the Bean methods described above. The result is then displayed to the user.
Here's what the result looks like:
Note that it's necessary to convert the type of the form variable $temp from string to integer in order to make it compatible with the arguments expected by the Bean - this data type conversion is one of the important issues you will face when accessing Java classes through PHP.
And that's about it for the moment. In case you'd like to learn more, take a look at the following links:
See you soon! Note: All examples in this article have been tested on Linux/i586 with JDK 1.3.0, Apache 1.3.20 and PHP 4.1.1. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!