php&java(二)


Posted in PHP onOctober 09, 2006

例子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很好的调用它!

PHP 相关文章推荐
一个简单的自动发送邮件系统(一)
Oct 09 PHP
php表单提交问题的解决方法
Apr 12 PHP
PHP中如何调用webservice的实例参考
Apr 25 PHP
初识PHP
Sep 28 PHP
php中$_GET与$_POST过滤sql注入的方法
Nov 03 PHP
php中使用url传递数组的方法
Feb 11 PHP
php读取csv文件并输出的方法
Mar 14 PHP
php检测文本的编码
Jul 26 PHP
PHP安全下载文件的方法
Apr 07 PHP
php实现简单的权限管理的示例代码
Aug 25 PHP
PHP封装curl的调用接口及常用函数详解
May 31 PHP
浅谈PHP各环境下的伪静态配置
Mar 13 PHP
php&amp;java(一)
Oct 09 #PHP
php&amp;java(三)
Oct 09 #PHP
一个用于mysql的数据库抽象层函数库
Oct 09 #PHP
教你如何把一篇文章按要求分段
Oct 09 #PHP
全文搜索和替换
Oct 09 #PHP
转换中文日期的PHP程序
Oct 09 #PHP
PHP网上调查系统
Oct 09 #PHP
You might like
是否存在第一台收音机的说法
2021/03/01 无线电
让你的WINDOWS同时支持MYSQL4,MYSQL4.1,MYSQL5X
2006/12/06 PHP
php简单smarty入门程序实例
2015/06/11 PHP
php实现在多维数组中查找特定value的方法
2015/07/29 PHP
laravel实现上传图片并在页面显示的例子
2019/10/14 PHP
JavaScript的面向对象方法以及差别
2008/03/31 Javascript
window.location.href IE下跳转失效的解决方法
2014/03/27 Javascript
js实现网页标题栏闪烁提示效果实例分析
2014/11/20 Javascript
AngularJS内置指令
2015/02/04 Javascript
jquery UI Datepicker时间控件的使用及问题解决
2016/04/28 Javascript
Bootstrap前端开发案例二
2016/06/17 Javascript
微信小程序page的生命周期和音频播放及监听实例详解
2017/04/07 Javascript
微信小程序 蓝牙的实现实例代码
2017/06/27 Javascript
Angular如何引入第三方库的方法详解
2017/07/13 Javascript
纯js代码生成可搜索选择下拉列表的实例
2018/01/11 Javascript
详解js静态检查工具eslint配置文件
2018/11/23 Javascript
js笔试题-接收get请求参数
2019/06/15 Javascript
使用vue for时为什么要key【推荐】
2019/07/11 Javascript
React Hooks 实现和由来以及解决的问题详解
2020/01/17 Javascript
[03:06]V社市场总监Dota2项目负责人Erik专访:希望更多中国玩家加入DOTA2
2014/07/11 DOTA
在Python中处理XML的教程
2015/04/29 Python
详解Python import方法引入模块的实例
2017/08/02 Python
Python贪心算法实例小结
2018/04/22 Python
python3获取当前文件的上一级目录实例
2018/04/26 Python
python3实现多线程聊天室
2018/12/12 Python
python中有函数重载吗
2020/05/28 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
2020/06/08 Python
如何用Python 加密文件
2020/09/10 Python
pycharm远程连接服务器并配置python interpreter的方法
2020/12/23 Python
美国领先的汽车轮胎和轮毂供应商:TireBuyer
2016/07/21 全球购物
手机配件第一品牌:ZAGG
2017/05/28 全球购物
环境卫生倡议书
2014/08/29 职场文书
国家税务局干部作风整顿整改措施
2014/09/18 职场文书
大学生军训自我鉴定范文
2014/09/18 职场文书
python基础之爬虫入门
2021/05/10 Python
教你怎么用Python实现GIF动图的提取及合成
2021/06/15 Python