Author: Well in the month
Example 1: Create and use your own Java class
It's very easy to create your own Java classes. Create a new Phptest.java file and place it in your Java.class.path directory with the following file contents:
public class phptest{
/**
* A sample of a class that can work with PHP
* Nb:the whole class must is public to work,
* And of course the methods you wish
* directly.
*
* Also Note that from PHP the Main method
* Won't is 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 are 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 will compile this file and use the Javac Phptest.java command on the DOS command line.
To test this Java class with PHP, we create a phptest.php file with the following content:
$myj = new Java ("Phptest");
echo "Test Results is". $myj->test ("Hello World"). "";
$myj->foo = "A String Value";
echo "You had set Foo to". $myj->foo. "
n ";
echo "My Java Method reports:". $myj->whatisfoo (). "
n ";
?>
If you get this warning message: java.lang.ClassNotFoundException error, this means 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 lead to errors, so when we pass the variable to Java, we should 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 to call it nicely!
http://www.bkjia.com/PHPjc/629325.html www.bkjia.com true http://www.bkjia.com/PHPjc/629325.html techarticle Author: Well month example 1: It is easy to create and use your own Java classes to create your own Java classes. Create a new Phptest.java file, place it in your Java.class.path directory, ...