php&java (2)_PHP tutorial

WBOY
Release: 2016-07-21 16:04:41
Original
1008 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.
*/ 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 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 something like this Warning message: 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 It's nice to call it using PHP!


http://www.bkjia.com/PHPjc/315886.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315886.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!