創建你自己的JAVA類非常容易。新建一個phptest.java文件,將它放置在你的java.class.path目錄下,文件內容如下:

public class phptest{
/**
* A sample of a class that can work wit " /> 欧美最猛黑人xxxxx猛交,精品国产免费人成电影在线观... 精品国产伦一区二区三区免费 ,成人免费观看在线观看

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php&java(二)

例子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很好的調用它!

php技術php&amp;amp;java(二),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 册亨县| 八宿县| 连城县| 比如县| 丘北县| 乡城县| 许昌县| 满城县| 石楼县| 贡嘎县| 锦屏县| 鄂伦春自治旗| 长兴县| 读书| 信丰县| 正定县| 安庆市| 区。| 东港市| 保靖县| 勐海县| 江川县| 乌兰察布市| 宁远县| 论坛| 高陵县| 旺苍县| 赣榆县| 任丘市| 阿克苏市| 和田市| 新郑市| 玛曲县| 南岸区| 额敏县| 平乡县| 东方市| 祥云县| 平泉县| 军事| 报价|