文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>PHP&Java(2)

PHP&Java(2)

时间:2007-02-17  来源:PHP爱好者

Example 1: Creating and using your own ClassesCreating 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; 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
www.c hinaitpower.comlKqhaTIslBj3xcnA0TLL4helDM
$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. chinaitpower.comKqhaTIslBj3xcnA0TLL4helDM 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. chinaitpower.comKqhaTIslBj3xcnA0TLL4helDM
php爱好者站 http://www.phpfans.net php动态|php基础|php进阶|php模板.
相关阅读 更多 +
排行榜 更多 +
找茬脑洞的世界安卓版

找茬脑洞的世界安卓版

休闲益智 下载
滑板英雄跑酷2手游

滑板英雄跑酷2手游

休闲益智 下载
披萨对对看下载

披萨对对看下载

休闲益智 下载