PHP
  Home arrow PHP arrow Page 3 - Using PHP with Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Using PHP with Java
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2002-04-03


    Table of Contents:
  • Using PHP with Java
  • Getting Started
  • Rank And File
  • A Custom Job
  • Passing The Parcel
  • An Exceptionally Clever Cow
  • Beanie Baby

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using PHP with Java - Rank And File
    ( Page 3 of 7 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT