国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php&java(二)

2024-05-04 23:00:35
字體:
來源:轉載
供稿:網友
菜鳥學堂:
例子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很好的調用它!
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿州市| 垣曲县| 邢台县| 美姑县| 安丘市| 临安市| 民乐县| 太保市| 汪清县| 江源县| 尚志市| 疏勒县| 吉隆县| 新泰市| 铜川市| 白水县| 榕江县| 临湘市| 淮南市| 班玛县| 永城市| 绥棱县| 南川市| 常熟市| 微山县| 赣州市| 高安市| 宁化县| 宜都市| 黄梅县| 石门县| 玛纳斯县| 常德市| 来凤县| 南昌市| 甘肃省| 福建省| 滨海县| 兰溪市| 栾川县| 兖州市|