Home arrow PHP arrow Page 4 - Using PHP with Java

A Custom Job - PHP

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.

  1. Using PHP with Java
  2. Getting Started
  3. Rank And File
  4. A Custom Job
  5. Passing The Parcel
  6. An Exceptionally Clever Cow
  7. Beanie Baby
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 17
April 03, 2002

print this article


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:

import java.io.*; public class FileReader { public File LocalFile; // set file location public void loadFile(String FileName) { LocalFile = new File(FileName); } // return file size public long getFileSize() { return LocalFile.length(); } // check if file exists public boolean FileExists() { return LocalFile.exists(); } // return file path public String getFilePath() { return LocalFile.getAbsolutePath(); } }
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.

<?php $myClass = new Java("FileReader"); $myClass->loadFile("/home/john/test.txt"); if($myClass->FileExists()) { echo "File size is " . $myClass->getFileSize() . "<br>"; echo "File path is " . $myClass->getFilePath(); } else { echo "Sorry, the file " . $myClass->getFilePath() . " could not be found"; } ?>
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:

File size is 385 File path is /home/john/test.txt
Next up, passing one Java object to another.

>>> More PHP Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: