日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

php java if_phpjava(二)

發布時間:2025/3/20 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php java if_phpjava(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

php&java(二)

更新時間:2006年10月09日 00:00:00 ? 作者:

例子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文件,內容如下:

$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";

?>

如果你得到這樣的警告信息:java.lang.ClassNotFoundException error ,這就意味著你的phptest.class文件不在你的java.class.path目錄下。

注意的是JAVA是一種強制類型語言,而PHP不是,這樣我們在將它們融合時,容易導致錯誤,于是我們在向JAVA傳遞變量時,要正確指定好變量的類型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";

這只是一個很小的例子,你可以創建你自己的JAVA類,并使用PHP很好的調用它!

相關文章

這篇文章主要介紹了windows8.1下Apache+Php+MySQL配置步驟,需要的朋友可以參考下2015-10-10

PHP4 與 MySQL 數據庫操作函數詳解...2006-10-10

Smarty實例教學 實例篇...2006-10-10

在 PHP 中使用隨機數的三個步驟...2006-10-10

php的require()與include(),在性能方面并無大的不同2006-10-10

以文本方式上傳二進制文件的PHP程序...2006-10-10

今天想向大家介紹PHP頁面間如何進行多條件組合查詢。在很多其它網站也有很多相關介紹,但都不夠詳盡,在這里,我將詳細地為大家說明這一比較實用但又相當關鍵的技術。2008-06-06

做一個有下拉功能的留言版...2006-10-10

前為目最全的CURL中文說明了,學PHP的要好好掌握.有很多的參數.大部份都很有用.真正掌握了它和正則,一定就是個采集高手了.2010-08-08

PHP5的XML新特性...2006-10-10

最新評論

總結

以上是生活随笔為你收集整理的php java if_phpjava(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。