PHP
  Home arrow PHP arrow Page 5 - 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 - Passing The Parcel
    ( Page 5 of 7 )

    This next example demonstrates how one object instance can be passed to another in a single PHP script.

    Here's the code:

    import java.io.Writer; import java.io.IOException; import java.lang.Exception; public class ReverseString { public Writer out; public void setWriter(Writer out) { this.out = out; } public void reverse(String string) throws Exception { try { // reverse the string here char tempArray[] = new char[string.length()]; for (int i = 0; i < string.length(); i++) tempArray[i] = string.charAt(string.length() - i - 1); String reversedString = new String(tempArray); out.write(reversedString); } catch (IOException e) { // catch IO exceptions throw new Exception("Something bad happened"); } catch (Exception e) { // catch everything else throw new Exception("Something really bad happened"); } } }
    This class uses a Writer object for script communication; this Writer object is instantiated via the setWriter() class method.

    public void setWriter(Writer out) { this.out = out; }
    Next, the class method reverse() accepts a string, reverses it and returns it via the Writer object.

    public void reverse(String string) throws Exception { try { // reverse the string here char tempArray[] = new char[string.length()]; for (int i = 0; i < string.length(); i++) tempArray[i] = string.charAt(string.length() - i - 1); String reversedString = new String(tempArray); out.write(reversedString); // snip } }
    Compile this class, copy it to your Java CLASSPATH, and write a simple PHP script to access it.

    <?php // create an instance of the ReverseString object $obj = new Java("ReverseString"); // create an instance of the StringWriter object $writer = new Java("java.io.StringWriter"); $obj->setWriter($writer); $obj->reverse("The cow jumped over the moon"); echo $writer->toString(); $writer->flush(); $writer->close(); ?>
    In this case, I've instantiated two objects, one for the ReverseString class and the other for the StringWriter class. Next, I've passed the StringWriter object, as stored in the PHP object $writer, to the ReverseString class via the class' setWriter() method, and then used the reverse() method to reverse a string and return the result.

    Here's what the output looks like:

    noom eht revo depmuj woc ehT


     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT