php&java(二)
更新時(shí)間:2006年10月09日 00:00:00 作者:
例子1:創(chuàng)建和使用你自己的JAVA類(lèi)
創(chuàng)建你自己的JAVA類(lèi)非常容易。新建一個(gè)phptest.java文件,將它放置在你的java.class.path目錄下,文件內(nèi)容如下:
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)建這個(gè)文件后,我們要編譯好這個(gè)文件,在DOS命令行使用javac phptest.java這個(gè)命令。
為了使用PHP測(cè)試這個(gè)JAVA類(lèi),我們創(chuàng)建一個(gè)phptest.php文件,內(nèi)容如下:
<?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是一種強(qiáng)制類(lèi)型語(yǔ)言,而PHP不是,這樣我們?cè)趯⑺鼈內(nèi)诤蠒r(shí),容易導(dǎo)致錯(cuò)誤,于是我們?cè)谙騄AVA傳遞變量時(shí),要正確指定好變量的類(lèi)型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";
這只是一個(gè)很小的例子,你可以創(chuàng)建你自己的JAVA類(lèi),并使用PHP很好的調(diào)用它!
創(chuàng)建你自己的JAVA類(lèi)非常容易。新建一個(gè)phptest.java文件,將它放置在你的java.class.path目錄下,文件內(nèi)容如下:
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)建這個(gè)文件后,我們要編譯好這個(gè)文件,在DOS命令行使用javac phptest.java這個(gè)命令。
為了使用PHP測(cè)試這個(gè)JAVA類(lèi),我們創(chuàng)建一個(gè)phptest.php文件,內(nèi)容如下:
<?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是一種強(qiáng)制類(lèi)型語(yǔ)言,而PHP不是,這樣我們?cè)趯⑺鼈內(nèi)诤蠒r(shí),容易導(dǎo)致錯(cuò)誤,于是我們?cè)谙騄AVA傳遞變量時(shí),要正確指定好變量的類(lèi)型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";
這只是一個(gè)很小的例子,你可以創(chuàng)建你自己的JAVA類(lèi),并使用PHP很好的調(diào)用它!
相關(guān)文章
無(wú)數(shù)據(jù)庫(kù)的詳細(xì)域名查詢程序PHP版(5)
無(wú)數(shù)據(jù)庫(kù)的詳細(xì)域名查詢程序PHP版(5)...2006-10-10PHP5權(quán)威編程閱讀學(xué)習(xí)筆記 附電子書(shū)下載
PHP5中,使用新的統(tǒng)一的構(gòu)造函數(shù)命名方式:__construct(),當(dāng)然,使用類(lèi)名同樣也是可以的2012-07-07PHP和Mysqlweb應(yīng)用開(kāi)發(fā)核心技術(shù) 第1部分 Php基礎(chǔ)-3 代碼組織和重用2
創(chuàng)建可以調(diào)用的函數(shù)以便重用代碼把參數(shù)傳遞給函數(shù)并且從函數(shù)返回值和腳本的不同部分中的變量和數(shù)據(jù)進(jìn)行交互2011-07-07

在PHP中利用XML技術(shù)構(gòu)造遠(yuǎn)程服務(wù)(下)
在PHP中利用XML技術(shù)構(gòu)造遠(yuǎn)程服務(wù)(下)...
2006-10-10