<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >PHP / Java Integration</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Documentaþia funcþiilor" 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=ISO-8859-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.ircg-whois.html" ACCESSKEY="P" >Înapoi</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" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="reference" ><A NAME="ref.java" ></A ><DIV CLASS="TITLEPAGE" ><H1 CLASS="title" >XLIX. PHP / Java Integration</H1 ><DIV CLASS="PARTINTRO" ><A NAME="AEN47816" ></A ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.intro" >Introducere</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 >Avertisment</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P >Aceastã extensie este <SPAN CLASS="emphasis" ><I CLASS="emphasis" >EXPERIMENTALÃ</I ></SPAN >. Comportamentul acestei extensii, incluzând numele funcþiilor ºi orice altceva documentat despre aceastã extensie ar putea fi schimbate farã un anunþ prealabil, intr-o viitoare versiune PHP. V-am avertizat, deci folosiþi aceastã extensie pe riscul dv.</P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.requirements" >Necesitãþi</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" >Instalare</A ></H1 ><P > Build instructions for this extension can be found in <TT CLASS="filename" >php4/ext/java/README</TT >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.configuration" >Configurare la rulare</A ></H1 ><P > The behaviour of these functions is affected by settings in <TT CLASS="filename" >php.ini</TT >. </P ><P > <DIV CLASS="table" ><A NAME="AEN47837" ></A ><P ><B >Tabel 1. Java configuration options</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >Name</TH ><TH >Default</TH ><TH >Changeable</TH ></TR ></THEAD ><TBODY ><TR ><TD >java.class.path</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ></TR ><TR ><TD >java.home</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ></TR ><TR ><TD >java.library.path</TD ><TD >NULL</TD ><TD >PHP_INI_ALL</TD ></TR ><TR ><TD >java.library</TD ><TD >JAVALIB</TD ><TD >PHP_INI_ALL</TD ></TR ></TBODY ></TABLE ></DIV > For further details and definition of the PHP_INI_* constants see <A HREF="function.ini-set.html" ><B CLASS="function" >ini_set()</B ></A >. </P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.resources" >Tipuri de resurse</A ></H1 ><P >Aceastã extensie nu defineºte tipuri de resurse.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.constants" >Constante predefinite</A ></H1 ><P >Aceastã extensie nu defineºte constante.</P ></DIV ><DIV CLASS="section" ><H1 CLASS="section" ><A NAME="java.examples" >Exemple</A ></H1 ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN47872" ></A ><P ><B >Exemplu 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">print </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 /> print </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 /> print </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 /> print </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="AEN47875" ></A ><P ><B >Exemplu 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 intented 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 > <VAR CLASS="literal" >new Java()</VAR > 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 <VAR CLASS="literal" >java.lang.System</VAR > 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, <VAR CLASS="literal" >print $date.time</VAR > will first attempt to be resolved as <VAR CLASS="literal" >$date.getTime()</VAR >, then as <VAR CLASS="literal" >$date.time</VAR >. </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 <VAR CLASS="literal" >java.lang.Class</VAR >, 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 cooerced 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 advanatage 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 >Cuprins</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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.java-last-exception-clear.html" ACCESSKEY="N" >Înainte</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" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >java_last_exception_clear</TD ></TR ></TABLE ></DIV ></BODY ></HTML >