<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PHP / Java Integration</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="函数参考" HREF="funcref.html"><LINK REL="PREVIOUS" TITLE="ircg_whois" HREF="function.ircg-whois.html"><LINK REL="NEXT" TITLE="java_last_exception_clear" HREF="function.java-last-exception-clear.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="reference" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ircg-whois.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.java-last-exception-clear.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.java" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >LXXII. PHP / Java Integration</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN103026" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.intro" >简介</A ></H1 ><P > There are two possible ways to bridge PHP and Java: you can either <A HREF="ref.java.html#java.servlet" >integrate PHP into a Java Servlet environment</A >, which is the more stable and efficient solution, or integrate Java support into PHP. The former is provided by a SAPI module that interfaces with the Servlet server, the latter by this Java extension. </P ><P > The Java extension provides a simple and effective means for creating and invoking methods on Java objects from PHP. The JVM is created using JNI, and everything runs in-process. </P ><DIV CLASS="warning" ><P ></P ><TABLE CLASS="warning" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >警告</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >本扩展模块是<SPAN CLASS="emphasis" ><I CLASS="emphasis" >实验性</I ></SPAN >的。本模块的行为,包括其函数的名称以及其它任何关于此模块的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。使用本扩展模块风险自担。</P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.requirements" >需求</A ></H1 ><P > You need a Java VM installed on your machine to use this extension. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.installation" >安装</A ></H1 ><P > 本 <A HREF="http://pecl.php.net" TARGET="_top" >PECL</A > 扩展未绑定于 PHP 中。 </P ><P > 在 PHP 4 中本 <ACRONYM CLASS="acronym" >PECL</ACRONYM > 扩展的源程序位于 PHP 源程序中的 <TT CLASS="filename" >ext/</TT > 目录下或者在上面的 <ACRONYM CLASS="acronym" >PECL</ACRONYM > 连接中。 In order to use these functions you must compile PHP with Java support by using the <CODE CLASS="option" >--with-java[=DIR]</CODE > where DIR points to the base install directory of your JDK. This extension can only be built as a shared extension. Additional build extensions can be found in <TT CLASS="filename" >php-src/ext/java/README</TT >. </P ><P > Windows users will enable <TT CLASS="filename" >php_java.dll</TT > inside of <TT CLASS="filename" >php.ini</TT > in order to use these functions. 在 PHP 4 中本 <ACRONYM CLASS="acronym" >DLL</ACRONYM > 位于 PHP Windows 执行包中的 <TT CLASS="filename" >extensions/</TT > 目录下。 可以从 <A HREF="http://www.php.net/downloads.php" TARGET="_top" >PHP 下载页面</A >或者 <A HREF="http://snaps.php.net/" TARGET="_top" >http://snaps.php.net/</A > 下载此 <ACRONYM CLASS="acronym" >PECL</ACRONYM > 扩展的 <ACRONYM CLASS="acronym" >DLL</ACRONYM > 文件。 </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > In order to enable this module on a Windows environment with PHP <= 4.0.6, you must make <TT CLASS="filename" >jvm.dll</TT > available to your systems PATH. No additional DLL is needed for PHP versions > 4.0.6. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.configuration" >运行时配置</A ></H1 ><P >这些函数的行为受 <TT CLASS="filename" >php.ini</TT > 的影响。</P ><P > <DIV CLASS="table" ><A NAME="AEN103065" ></A ><P ><B >表 1. Java configuration options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Default</TH ><TH >Changeable</TH ><TH >Changelog</TH ></TR ></THEAD ><TBODY ><TR ><TD >java.class.path</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >java.home</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >java.library.path</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ><TR ><TD >java.library</TD ><TD >JAVALIB</TD ><TD >PHP_INI_ALL</TD ><TD > </TD ></TR ></TBODY ></TABLE ></DIV > 有关 PHP_INI_* 常量进一步的细节与定义参见<A HREF="ini.html" >附录 H</A >。 </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.resources" >资源类型</A ></H1 ><P >本扩展模块未定义任何资源类型。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.constants" >预定义常量</A ></H1 ><P >本扩展模块未定义任何常量。</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.examples" >范例</A ></H1 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN103105" ></A ><P ><B >例 1. Java Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// get instance of Java class java.lang.System in PHP<br /></font><font color="#0000BB">$system </font><font color="#007700">= new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.lang.System'</font><font color="#007700">);<br /><br /></font><font color="#FF8000">// demonstrate property access<br /></font><font color="#007700">echo </font><font color="#DD0000">'Java version=' </font><font color="#007700">. </font><font color="#0000BB">$system</font><font color="#007700">-></font><font color="#0000BB">getProperty</font><font color="#007700">(</font><font color="#DD0000">'java.version'</font><font color="#007700">) . </font><font color="#DD0000">'<br />'</font><font color="#007700">;<br />echo </font><font color="#DD0000">'Java vendor=' </font><font color="#007700">. </font><font color="#0000BB">$system</font><font color="#007700">-></font><font color="#0000BB">getProperty</font><font color="#007700">(</font><font color="#DD0000">'java.vendor'</font><font color="#007700">) . </font><font color="#DD0000">'<br />'</font><font color="#007700">;<br />echo </font><font color="#DD0000">'OS=' </font><font color="#007700">. </font><font color="#0000BB">$system</font><font color="#007700">-></font><font color="#0000BB">getProperty</font><font color="#007700">(</font><font color="#DD0000">'os.name'</font><font color="#007700">) . </font><font color="#DD0000">' ' </font><font color="#007700">.<br /> </font><font color="#0000BB">$system</font><font color="#007700">-></font><font color="#0000BB">getProperty</font><font color="#007700">(</font><font color="#DD0000">'os.version'</font><font color="#007700">) . </font><font color="#DD0000">' on ' </font><font color="#007700">.<br /> </font><font color="#0000BB">$system</font><font color="#007700">-></font><font color="#0000BB">getProperty</font><font color="#007700">(</font><font color="#DD0000">'os.arch'</font><font color="#007700">) . </font><font color="#DD0000">' <br />'</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// java.util.Date example<br /></font><font color="#0000BB">$formatter </font><font color="#007700">= new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.text.SimpleDateFormat'</font><font color="#007700">,<br /> </font><font color="#DD0000">"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$formatter</font><font color="#007700">-></font><font color="#0000BB">format</font><font color="#007700">(new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.util.Date'</font><font color="#007700">));<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN103108" ></A ><P ><B >例 2. AWT Example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#FF8000">// This example is only intended to be run as a CGI.<br /><br /></font><font color="#0000BB">$frame </font><font color="#007700">= new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.awt.Frame'</font><font color="#007700">, </font><font color="#DD0000">'PHP'</font><font color="#007700">);<br /></font><font color="#0000BB">$button </font><font color="#007700">= new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.awt.Button'</font><font color="#007700">, </font><font color="#DD0000">'Hello Java World!'</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$frame</font><font color="#007700">-></font><font color="#0000BB">add</font><font color="#007700">(</font><font color="#DD0000">'North'</font><font color="#007700">, </font><font color="#0000BB">$button</font><font color="#007700">);<br /></font><font color="#0000BB">$frame</font><font color="#007700">-></font><font color="#0000BB">validate</font><font color="#007700">();<br /></font><font color="#0000BB">$frame</font><font color="#007700">-></font><font color="#0000BB">pack</font><font color="#007700">();<br /></font><font color="#0000BB">$frame</font><font color="#007700">-></font><font color="#0000BB">visible </font><font color="#007700">= </font><font color="#0000BB">True</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$thread </font><font color="#007700">= new </font><font color="#0000BB">Java</font><font color="#007700">(</font><font color="#DD0000">'java.lang.Thread'</font><font color="#007700">);<br /></font><font color="#0000BB">$thread</font><font color="#007700">-></font><font color="#0000BB">sleep</font><font color="#007700">(</font><font color="#0000BB">10000</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$frame</font><font color="#007700">-></font><font color="#0000BB">dispose</font><font color="#007700">();<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > Notes: <P ></P ><UL ><LI ><P > <TT CLASS="literal" >new Java()</TT > will create an instance of a class if a suitable constructor is available. If no parameters are passed and the default constructor is useful as it provides access to classes like <TT CLASS="literal" >java.lang.System</TT > which expose most of their functionallity through static methods. </P ></LI ><LI ><P > Accessing a member of an instance will first look for bean properties then public fields. In other words, <TT CLASS="literal" >print $date.time</TT > will first attempt to be resolved as <TT CLASS="literal" >$date.getTime()</TT >, then as <TT CLASS="literal" >$date.time</TT >. </P ></LI ><LI ><P > Both static and instance members can be accessed on an object with the same syntax. Furthermore, if the java object is of type <TT CLASS="literal" >java.lang.Class</TT >, then static members of the class (fields and methods) can be accessed. </P ></LI ><LI ><P > Exceptions raised result in PHP warnings, and <TT CLASS="constant" ><B >NULL</B ></TT > results. The warnings may be eliminated by prefixing the method call with an "@" sign. The following APIs may be used to retrieve and reset the last error: <P ></P ><UL ><LI ><P ><A HREF="function.java-last-exception-get.html" ><B CLASS="function" >java_last_exception_get()</B ></A ></P ></LI ><LI ><P ><A HREF="function.java-last-exception-clear.html" ><B CLASS="function" >java_last_exception_clear()</B ></A ></P ></LI ></UL > </P ></LI ><LI ><P > Overload resolution is in general a hard problem given the differences in types between the two languages. The PHP Java extension employs a simple, but fairly effective, metric for determining which overload is the best match. </P ><P > Additionally, method names in PHP are not case sensitive, potentially increasing the number of overloads to select from. </P ><P > Once a method is selected, the parameters are coerced if necessary, possibly with a loss of data (example: double precision floating point numbers will be converted to boolean). </P ></LI ><LI ><P > In the tradition of PHP, arrays and hashtables may pretty much be used interchangably. Note that hashtables in PHP may only be indexed by integers or strings; and that arrays of primitive types in Java can not be sparse. Also note that these constructs are passed by value, so may be expensive in terms of memory and time. </P ></LI ></UL > </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.servlet" >Java Servlet SAPI</A ></H1 ><P > The Java Servlet SAPI builds upon the mechanism defined by the Java extension to enable the entire PHP processor to be run as a servlet. The primary advantage of this from a PHP perspective is that web servers which support servlets typically take great care in pooling and reusing JVMs. Build instructions for the Servlet SAPI module can be found in <TT CLASS="filename" >php4/sapi/README</TT >. Notes: <P ></P ><UL ><LI ><P > While this code is intended to be able to run on any servlet engine, it has only been tested on Apache's Jakarta/tomcat to date. Bug reports, success stories and/or patches required to get this code to run on other engines would be appreciated. </P ></LI ><LI ><P > PHP has a habit of changing the working directory. sapi/servlet will eventually change it back, but while PHP is running the servlet engine may not be able to load any classes from the CLASSPATH which are specified using a relative directory syntax, or find the work directory used for administration and JSP compilation tasks. </P ></LI ></UL > </P ></DIV ></DIV ><DIV CLASS="TOC" ><DL ><DT ><B >目录</B ></DT ><DT ><A HREF="function.java-last-exception-clear.html" >java_last_exception_clear</A > -- Clear last Java exception</DT ><DT ><A HREF="function.java-last-exception-get.html" >java_last_exception_get</A > -- Get last Java exception</DT ></DL ></DIV ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.ircg-whois.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.java-last-exception-clear.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >ircg_whois</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="funcref.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >java_last_exception_clear</TD ></TR ></TABLE ></DIV ></BODY ></HTML >