最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php&java(二)

 更新時間:2006年10月09日 00:00:00   作者:  
例子1:創(chuàng)建和使用你自己的JAVA類
創(chuàng)建你自己的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));   
}
}
}
}

創(chuàng)建這個文件后,我們要編譯好這個文件,在DOS命令行使用javac phptest.java這個命令。

為了使用PHP測試這個JAVA類,我們創(chuàng)建一個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";

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

相關文章

最新評論

嘉兴市| 栾川县| 英吉沙县| 临汾市| 泰宁县| 万安县| 阳东县| 长葛市| 沛县| 汽车| 红桥区| 大兴区| 沙坪坝区| 水城县| 平阴县| 甘南县| 南涧| 海南省| 德清县| 雷波县| 淮滨县| 沧州市| 黑水县| 卢湾区| 华容县| 丰县| 修文县| 福建省| 棋牌| 营山县| 盐城市| 新兴县| 任丘市| 南雄市| 武宣县| 肃北| 丹东市| 普兰店市| 外汇| 绥棱县| 利辛县|