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 相关文章推荐
在PHP中利用XML技术构造远程服务(下)
Oct 09 PHP
php实现的MySQL通用查询程序
Mar 11 PHP
php计算十二星座的函数代码
Aug 21 PHP
用C/C++扩展你的PHP 为你的php增加功能
Sep 06 PHP
Drupal7中常用的数据库操作实例
Mar 02 PHP
php获取发送给用户的header信息的方法
Mar 16 PHP
PHP四种基本排序算法示例
Apr 09 PHP
ubuntu下配置nginx+php+mysql详解
Sep 10 PHP
php根据日期或时间戳获取星座信息和生肖等信息
Oct 20 PHP
thinkphp框架下404页面设置 仅三步
May 14 PHP
PHP生成word文档的三种实现方式
Nov 14 PHP
php实现水印文字和缩略图的方法示例
Dec 29 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
PHP中动态显示签名和ip原理
2007/03/28 PHP
php iconv() : Detected an illegal character in input string
2010/12/05 PHP
php中curl使用指南
2015/02/05 PHP
使用PHP如何实现高效安全的ftp服务器(二)
2015/12/30 PHP
解决PHP curl或file_get_contents下载图片损坏或无法打开的问题
2019/10/11 PHP
php正则表达式使用方法整理集合
2020/01/31 PHP
采用call方式实现js继承
2014/05/20 Javascript
深入理解javascript严格模式(Strict Mode)
2014/11/28 Javascript
js判断手机和pc端选择不同执行事件的方法
2015/01/30 Javascript
JS实现超炫网页烟花动画效果的方法
2015/03/02 Javascript
JS如何判断是否为ie浏览器的方法(包括IE10、IE11在内)
2015/12/13 Javascript
jQuery+css实现的tab切换标签(兼容各浏览器)
2016/01/28 Javascript
JavaScript实战(原生range和自定义特效)简单实例
2016/08/21 Javascript
Vuex之理解Mutations的用法实例
2017/04/19 Javascript
12个非常有用的JavaScript技巧
2017/05/17 Javascript
浅谈mint-ui loadmore组件注意的问题
2017/11/08 Javascript
使用vue-router beforEach实现判断用户登录跳转路由筛选功能
2018/06/25 Javascript
React之PureComponent的使用作用
2018/07/10 Javascript
JS中创建自定义类型的常用模式总结【工厂模式,构造函数模式,原型模式,动态原型模式等】
2019/01/19 Javascript
three.js利用卷积法如何实现物体描边效果
2019/11/27 Javascript
在react项目中使用antd的form组件,动态设置input框的值
2020/10/24 Javascript
python网络编程学习笔记(六):Web客户端访问
2014/06/09 Python
python操作ssh实现服务器日志下载的方法
2015/06/03 Python
Python合并字典键值并去除重复元素的实例
2016/12/18 Python
基于python代码实现简易滤除数字的方法
2018/07/17 Python
python3正则提取字符串里的中文实例
2019/01/31 Python
python中append实例用法总结
2019/07/30 Python
Python Django中间件,中间件函数,全局异常处理操作示例
2019/11/08 Python
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
英国著名的茶叶品牌:Whittard of Chelsea
2016/09/22 全球购物
数据库设计的包括哪两种,请分别进行说明
2016/07/15 面试题
成教自我鉴定
2013/10/27 职场文书
小学教师办公室制度
2014/02/03 职场文书
自我工作评价范文
2015/03/06 职场文书
小学生心理健康活动总结
2015/05/08 职场文书
导游词之井冈山
2019/11/20 职场文书