仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1815|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网站制作之php&java(二)

[复制链接]
第二个灵魂 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-4 00:29:16 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
看到好的帖子最好up一下,以使得更多的人得到分享。   例子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很好的挪用它!


  会HTML吗?会,我能编好几个大表格排板的网页啦!
第二个灵魂 该用户已被删除
沙发
 楼主| 发表于 2015-3-23 21:54:06 | 显示全部楼层
环境搭建好,当你看见你的浏览器输出“it works\\\\\\\"时你一定是喜悦的。在你解决问题的时候,我强烈建议多读php手册。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-13 17:20

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表