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

14.2.3     Your own classes

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

The next step forward is to use Java to create your own classes, compile that using javac, then load the class into PHP for use. This is better than feeding Java code in line by line because you can put a lot of your application logic into the Java class, which is then properly compiled down into byte code. You can then simply call just one function from your PHP script to do a lot of Java processing.

Consider the following Java code, factor.java, which calculates the factorial of a number:

public class factor {
    public int factor(int num) {
        if ((num == 0) || (num == 1)) return 1;
        return num * factor(num - 1);
    }
}

We looked at calculating factorials many chapters ago, so I shan't explain again what they are. This time around there is the slight optimisation that we also do not bother recursing the function if the number to calculate is 1.

Once that file is compiled, we will have factor.class, our own Java class file. This should be copied to the location you entered into your php.ini file as your class path, thereby making the class file available for us to use in PHP. When creating a new Java object, Java will search for the class file for that object in several places, the last of which is the directory specified by your php.ini file. As a result, we can create an object of this class with the following code:

$myfactor = new Java('factor');

As you can see, the main class is called "factor", so that is what we use to create the object. The main (and only!) function is also called factor, and takes an integer parameter to calculate. Therefore, here is the complete PHP script to calculate factors using the Java factor class we created:

<?php
    $factor
= new Java('factor');
    print
$factor->factor(4);
?>

As you can see there are no big surprises here - the factor() function of our factor object is called, passing in 4. This should return 24, because 4 x 3 x 2 x 1 is 24. Note that if you want to use large values with this, you should modify the Java class to return a double-precision floating-point number, because integers do not go very high.





<< 14.2.2 The drawbacks of basic Java use   14.2.4 Using Swing >>
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 four plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow