How to Convert From Java to PHP

Java and PHP are two of the most used programming languages today. Although both technologies offer advanced functionality and dynamic portability, converting one to the other is not quite possible yet. The good news though is that Java and PHP classes can be integrated together to form efficient applications, such as for sales tax calculation. The main script to be used for this PHP-Java integration project is PHP script and the compiler will be Java.

...
Java integrates well with PHP.

Step

Paste the code below to establish your Java compiler, which you will need to compile your salesTax.java project within the PHP script:

Step

import java.util.; import java.text.;

Step

public class SalesTax {

Step

public String SalesTax(double price, double salesTax) {

Step

numberFormatter.format(salesTax);

Step

}

Step

Run PHP to call the compiled code called SalesTaxInterface.php. This function will calculate the data inputted by the user in your HTML form:

Step

<?php

Step

// Format the HTML form.

Step

$salesTaxForm = <<<SalesTaxForm

Step

Step

Price (ex. 42.56):

Step


Step

Sales Tax rate (ex. 0.06):

Step


Step

Step

Step

SalesTaxForm;

Step

if (! isset($submit)) :

Step

echo $salesTaxForm;

Step

else :

Step

// Instantiate the SalesTax class. $salesTax = new Java("SalesTax");

Step

// Don't forget to typecast in order to

Step

// conform with the Java method specifications.

Step

$price = (double) $price; $tax = (double) $tax;

Step

print $salesTax->SalesTax($price, $tax);

Step

endif;

Step

?>

Step

Add the following code to your project to bridge Java and PHP together:

Step

#!/bin/env php <?php

Step

require_once("java/Java.inc");

Step

include("wsimport.php");

Step

try { $addNumbersService = new java("org.duke.AddNumbersService"); $port = $addNumbersService->getAddNumbersPort(); $number1 = 10; $number2 = 20; echo ("Invoking one-way operation. Nothing is returned from service.\n"); $port->oneWayInt($number1); echo ("Invoking addNumbers($number1, $number2)\n"); $result = $port->addNumbers($number1, $number2); echo ("The result of adding $number1 and $number2 is $result\n\n"); $number1 = -10; echo ("Invoking addNumbers($number1, $number2)\n"); $result = $port->addNumbers($number1, $number2); echo ("The result of adding $number1 and $number2 is $result\n\n"); } catch (JavaException $ex) { $ex = $ex->getCause(); if (java_instanceof($ex, java("org.duke.AddNumbersFault_Exception"))) { $info = $ex->getFaultInfo()->getFaultInfo (); echo ("Caught AddNumbersFault_Exception: $ex, INFO: $info.\n"); } else { echo ("Exception occured: $ex\n"); } } ?>