samantha thavasa php&java(二)

WBOY
Release: 2016-07-29 08:33:41
Original
1065 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 < args. length; i++) {
String arg = args[i];
System.out.println(p.test(arg));
}
}
}
}
After creating this file, we need to compile it, 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() . "?>
If you get this warning message: java.lang.ClassNotFoundException error, it means that your phptest.class file is not in your java.class.path directory.
Please 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!

The above introduces samantha thavasa php&java (2), including the content of samantha thavasa. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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!