php&java(二)
2006-10-09 00:00
609 查看
例子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很好的调用它!
创建你自己的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很好的调用它!
相关文章推荐
- JAVA学习路线(转自:http://bbs.51cto.com/viewthread.php?tid=978308&extra=&page=1)
- PHP & Java(3)
- 编程开发(C/C++&Java&Python&JavaScript&Go&PHP&Ruby&Perl&R&Erlang)
- 我对View层的视图模板解析引擎的认识(Java&PHP)
- php&java(三)
- 支付宝官网下载App支付服务端DEMO&S(Java、.net、php 4000 )
- 设计模式 -- 单例模式(Java&&PHP)
- PHP & Java(4)
- PHP&Java 调用C#的WCF
- 使用PHPHessian调用Java Spring Hessian暴露服务­
- PHP 5, PHP/Java Bridge & Apache on XP PRO + SP2 and examples
- php&java(三)
- PHP & Java
- Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
- 设计模式(一)简单工厂(创建型)(Java&&PHP)
- 获取文件的md5(java & php)
- Atitit.ati dwr的原理and设计 attilax 总结 java php 版本
- php&java的面向对象选择排序
- 算法面试-基本题-排序 c++&java&php
- PHP & JAVA 实现 PBKDF2 加密算法