|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
例子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 " . $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很好的调用它! 返回类别: 教程 上一教程: 使用网络地址转变实现多服务器负载均衡 下一教程: 实例学习PHP之投票程序篇 您可以阅读与"php 与 java(二)"相关的教程: · 好用的php程序编辑器(要害字带颜色,支持其他文件类型,如htm,java,c等) · PHP与JavaScript对多项选择的处理 · 用PHP+java实现自动新闻滚动窗口 · PHP+Javascript模仿Matrix画面 · PHP和JAVA的XML-RPC中文问题解决办法 |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |