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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: