PHP & Java(2) (轉)
Example 1: Creating and using your own Classes
Creating your own class is pretty easy. Open a new text file called test.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; } /** * whatioo() 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 server called phptest.php. It should contain the following:
$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";
?>
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.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987798/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- PHP安全(2) (轉)PHP
- PHP & Java(4) (轉)PHPJava
- PHP & Java(3) (轉)PHPJava
- PHP & Java(1) (轉)PHPJava
- 應該要PHP轉Java了。。。PHPJava
- 求 PHP 轉 java 的相關教程PHPJava
- java之物件轉型2Java物件
- Java2HTML改造手記(2) (轉)JavaHTML
- PHP 轉 JAVA 篇 小插曲(環境篇)PHPJava
- PHP呼叫java類的兩種方法(轉)PHPJava
- XML的JAVA 解析(一)(2) (轉)XMLJava
- java編碼規範(2) (轉)Java
- [php]php總結(2)PHP
- 2、java資料型別轉換Java資料型別
- 第2個java示例程式(轉)Java
- Java Servlet和JSP教程(2)(轉)JavaServletJS
- 在C、JAVA、PHP中操作postgreSql資料庫 (轉)JavaPHPSQL資料庫
- Redhat linux 9下讓PHP支援java類(轉)RedhatLinuxPHPJava
- Redhat9.0 Apache2+Php5.0+Mysql4.0配置(轉)RedhatApachePHPMySql
- SMTH Java FAQ (2) 資料轉換(菜鳥必看) (轉)Java
- Java2Kotlin轉換自查表JavaKotlin
- Java2HTML改造手記(5) (轉)JavaHTML
- Java2HTML改造手記(7) (轉)JavaHTML
- Java2HTML改造手記(6) (轉)JavaHTML
- Java2HTML改造手記(續) (轉)JavaHTML
- Java2HTML改造手記(1) (轉)JavaHTML
- Java語言編碼規範(2)(轉)Java
- java截獲標準輸出(2)(轉)Java
- Java2HTML改造手記(4) (轉)JavaHTML
- Java2HTML改造手記(3) (轉)JavaHTML
- PHP轉Java有沒有好的學習路線PHPJava
- debian + apache2 + php4 +mysql +phpmyadmin + mt(轉)ApachePHPMySql
- php學習(2)PHP
- PHP and COM (轉)PHP
- Java學習從入門到精通(2)(轉)Java
- Java程式中的多執行緒(2)(轉)Java執行緒
- c/c++/java/go/php/python/rust..的本質2C++JavaGoPHPPythonRust
- Java 2原始碼解讀1:java.util.ArrayList (轉帖收集)Java原始碼