PHP & Java(2)
2001-01-09 15:29
861 查看
Example 1: Creating and using your own Classes
Creating your own Java class is pretty easy. Open a new text file called phptest.java. Make sure it is in your java.class.path, as specified in your php.ini file. In that file, enter the following: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)); } } } }
Once you have created the file, you want to compile it with javac phptest.java at the command line. This will depend on having the java/bin directory in your PATH environment variable. Once compiled, you can test the class on the command line. Try java phptest, or java phptest hello world. This class is made accessible on the command line, via the main() method. This isn't required for our Java + PHP explorations, but is a nice way to see the class at work.
To test this new class with PHP, create a new php file on your web server called phptest.php. It should contain the following:
<?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"; ?>
If you get a Warning: java.lang.ClassNotFoundException error, it simply means your phptest.class file is not in your java.class.path specified in your php.ini file.
One thing to remember is that Java is strongly typed, and PHP isn't. This could cause problems when Java expects a String, but receives an Integer (or visa-versa). Try replacing the line:
$myj->foo = "A String Value";with
$myj->foo = 12345678;and see what happens.
You should cast your variables to the correct type before passing them to Java. eg:
$myj->foo = (string) 12345678;or
$myj->foo = "12345678";
The phptest.java example is a very simple example of how you can create you own Java class and have PHP access to it.
相关文章推荐
- 数据结构(冒泡、快速、插入等排序)PHP && JAVA --面试技巧
- 我对View层的视图模板解析引擎的认识(Java&PHP)
- 设计模式(一)简单工厂(创建型)(Java&&PHP)
- atitit.js javascript 调用c# java php后台语言api html5交互的原理与总结p97
- 支付宝官网下载App支付服务端DEMO&S(Java、.net、php 4000 )
- 针对"不重新发送信息,则无法刷新网页。"的终极解决方法。(php&java范例)
- PHP & JAVA 实现 PBKDF2 加密算法
- php&java(二)
- JAVA连接ORACLE的基本步骤,写在这里做备忘 (参考了http://student.csdn.net/space.php?uid=412909&do=blog&id=33102)
- 编程开发(C/C++&Java&Python&JavaScript&Go&PHP&Ruby&Perl&R&Erlang)
- 算法面试-基本题-排序 c++&java&php
- php&java(一)
- 使用PHPHessian调用Java Spring Hessian暴露服务­
- PHP&Java 调用C#的WCF
- php&java(二)
- Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
- php&java(二)
- PHP & Java(1)
- PHP&Java(1)
- 获取文件的md5(java & php)