Home > Backend Development > PHP Tutorial > php and java (2)_PHP tutorial

php and java (2)_PHP tutorial

WBOY
Release: 2016-07-13 17:12:46
Original
767 people have browsed it

Author: Jing Zhongyue

Example 1: Create and use your own JAVA class
Creating your own JAVA class is very easy. Create a new phptest.java file and place it in your java.class.path directory. The file content is as follows:
public class phptest{
/**
* A sample of a class that can work with PHP
* NB: The whole class must be public to work,
* and of course the methods you wish to call
* directly.
*
* Also note that from PHP the main method
* will not 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 is 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(p.test(arg));
}else{
for (int i=0; i < args.length; i++) {
String arg = args[i];
System.out.println(p.test(arg));
}
}
}
}
After creating this file , we need to compile this file and use the javac phptest.java command on the DOS command line.
In order to use PHP to test this JAVA class, we create a phptest.php file with the following content:
$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 a warning message like this: java .lang.ClassNotFoundException error , which means that your phptest.class file is not in your java.class.path directory.
Note that JAVA is a typed language, but PHP is not. This can easily lead to errors when we integrate them. Therefore, when we pass variables to JAVA, we must correctly specify the type of the variable. Such as: $myj->foo = (string) 12345678; or $myj->foo = "12345678";
This is just a small example, you can create your own JAVA class and use PHP very Well call it!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629325.htmlTechArticleAuthor: Jing Zhongyue Example 1: Create and use your own JAVA classes Creating your own JAVA classes is very easy. Create a new phptest.java file and place it in your java.class.path directory,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template