关 键 词:
调用 如何 String phptest the public 文件 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文件,内容如下:
$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开发资源论坛讨论。
相关文章
图文推荐
论 坛 资 源
·Zope的优点及和Apache+PHP+MySQL的比较
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·黑防黑:黑客口述—关于Php后门的隐藏技巧
·PRADO框架TDataGrid使用教程(1)
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·黑防黑:黑客口述—关于Php后门的隐藏技巧
·PRADO框架TDataGrid使用教程(1)
热门技术文档
·Zope的优点及和Apache+PHP+MySQL的比较
·Windows 下的 PHP 扩展编程
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·php时间求法(二)
·Windows 下的 PHP 扩展编程
·PHP面向对象、类经典教程[2]
·PHP面向对象、类经典教程[1]
·用 Xdebug 修正 PHP 应用程序中的错误(5)
·用 Xdebug 修正 PHP 应用程序中的错误(4)
·用 Xdebug 修正 PHP 应用程序中的错误(3)
·用 Xdebug 修正 PHP 应用程序中的错误(2)
·用 Xdebug 修正 PHP 应用程序中的错误(1)
·php时间求法(二)
最新图文档
本站编辑推荐:(本站开通Delphi4PHP专区,欢迎进入论坛交流!)
- · 3分钟快速了解 Delphi for PHP 特色 (中文), PDF档
- · 购买Delphi for PHP的五大理由, PDF档
- · Delphi for PHP 使用规格介绍, PDF档
- · Delphi for PHP 問答集 (From CodeGear)
- · Delphi for PHP 产品价格表
编缉最近更新文章
网站赞助商
搜索您感兴趣的内容




