Now, the example on the previous page used a built-in Java class to demonstrate PHP/Java connectivity. It's also possible to instantiate and use a custom Java class in a PHP script. I'll demonstrate this by encapsulating the functionality of the core File class in my own FileReader class. Here's the code for my custom class: There's no rocket science here - this class is only a wrapper for core File class methods. However, it will serve to demonstrate the basics of using a custom Java class in a PHP script. Now, compile the class, copy the compiled code to your Java CLASSPATH, and write a script to use it. This is similar to the first example, except that, this time, I've used my own custom Java class in the script. Once an instance of the class has been instantiated, class methods can be accessed in the normal manner. Here's what the output looks like: Next up, passing one Java object to another.
blog comments powered by Disqus |