php and java 2_PHP tutorial

WBOY
Release: 2016-07-13 17:30:34
Original
863 people have browsed it

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. 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 String arg = args[i];
System.out.println(p.test(arg));
}
}
}
}
Create this After completing the file, we need to compile the file and use the javac phptest.java command on the DOS command line.
In order to test this JAVA class using PHP, 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 just fine Call it!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/509171.htmlTechArticleExample 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. The file content is as follows...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!