|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
看到好的帖子最好up一下,以使得更多的人得到分享。 例子1:创立和利用你本人的JAVA类
创立你本人的JAVA类十分轻易。新建一个phptest.java文件,将它放置在你的java.class.path目次下,文件内容以下:
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));
}
}
}
}
创立这个文件后,咱们要编译好这个文件,在DOS号令行利用javac phptest.java这个号令。
为了利用PHP测试这个JAVA类,咱们创立一个phptest.php文件,内容以下:
<?php
$myj = new Java("phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";
$myj->foo = "A String Value";
echo "You have set foo to <b>" . $myj->foo . "</b><br>n";
echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>n";
?>
假如你失掉如许的正告信息:java.lang.ClassNotFoundException error ,这就意味着你的phptest.class文件不在你的java.class.path目次下。
注重的是JAVA是一种强迫类型言语,而PHP不是,如许咱们在将它们交融时,轻易招致毛病,因而咱们在向JAVA传递变量时,要准确指定好变量的类型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";
这只是一个很小的例子,你可以创立你本人的JAVA类,并利用PHP很好的挪用它!
会HTML吗?会,我能编好几个大表格排板的网页啦! |
|