Home arrow PHP arrow Page 3 - Using PHP with Java

Rank And File - 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
Let's begin with a simple example to verify that everything is working as advertised. This script uses built-in Java methods to check for the existence of a particular file on your system and display its size.

<?php $fp = new Java("java.io.File", "/home/john/test.txt"); if($fp->exists()) { echo "The file ". $fp->getAbsolutePath() . " is ". $fp->length() . " bytes"; } else { echo "The file " . $fp->getAbsolutePath() . " does not exist"; } ?>
This is fairly simple, and should be clearly understandable to anyone who knows basic Java programming. The first line of the script instantiates an object of Java's File class, and stores it in the PHP variable $fp.

$fp = new Java("java.io.File", "/home/john/test.txt");
In case you're wondering, it's possible to pass parameters to the class constructor by specifying them as arguments when creating the object. In the example above, the second argument to the class constructor contains the location of the file to be displayed.

Once the object has been instantiated, it becomes possible to access the methods and properties of the Java class using regular OOP syntax.

if($fp->exists()) { echo "The file ". $fp->getAbsolutePath() . " is ". $fp->length() . " bytes"; } else { echo "The file " . $fp->getAbsolutePath() . " does not exist"; }
In this case, the exists(), getAbsolutePath() and length() methods are all methods of the File class; however, I've managed to use them in a PHP script by virtue of the Java connectivity built into PHP.

Here's what the output of the script above looks like:

The file /home/john/test.txt is 385 bytes


 
 
>>> 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 4 - Follow our Sitemap

Dev Shed Tutorial Topics: