PHP and Java (ii)

Source: Internet
Author: User
Tags command line empty php file php and return variable
Author: Well Middle Moon

Example 1: Creating and using your own Java class
It is easy to create your own Java class. Create a new Phptest.java file and place it in your Java.class.path directory, which reads as follows:

public class phptest{
/**
* A sample of a class that can work with PHP
* Nb:the whole class must to work,
* And of course the methods you wish to call
* directly.
*
* Also Note this from PHP's Main method
* Won't be called
*/

Public String foo;

/**
* Takes a string and returns the result
* or a msg saying your string was empty
*/
public string Test (string str) {
if (Str.equals ("")) {
str = "Your string was empty.";
}
return str;
}

/**
* Whatisfoo () simply returns the value of the variable foo.
*/
Public String Whatisfoo () {
Return ' foo is ' + foo;
}


/**
* This is called if phptest be run from the command line with
* Something like
* Java phptest
* OR
* Java phptest Hello there
*/
public static void Main (String args[]) {
Phptest p = new Phptest ();

if (Args.length = = 0) {
String arg = "";
System.out.println (ARG) (p.test);
}else{
for (int i=0 i < args.length; i++) {
String arg = args[i];
System.out.println (ARG) (p.test);
}
}
}
}

After creating this file, we will compile this file and use Javac Phptest.java This command at the DOS command line.

To test this Java class using PHP, we create a phptest.php file that reads as follows:


$myj = new Java ("Phptest");
echo "Test Results are". $myj->test ("Hello World"). "";

$myj->foo = "A String Value";
echo "You have set Foo to". $myj->foo. "
n ";
echo "My Java Method reports:". $myj->whatisfoo (). "
n ";

?>

If you get such a warning message: java.lang.ClassNotFoundException error, this means that your Phptest.class file is not in your Java.class.path directory.
Note that Java is a mandatory type language, and PHP is not, so that when we merge them, it is easy to cause errors, so when we are passing variables to Java, we need to correctly specify the type of the variable. such as: $myj->foo = (string) 12345678; or $myj->foo = "12345678";

This is just a small example where you can create your own Java class and use PHP to call it well!


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.