Java support represents
without a doubt one of the new features that will open PHP up to existing
enterprise systems and applications. With PHP4, users can
instantiate and use Java servlets or classes.
Contents |
Introduction |
Differences Between PHP3 and PHP4 |
Output Buffering |
Evaluate for Identical Operator |
COM support on Windows |
Displaying Portions of Raw HTML Code |
Java Support |
License |
Conclusion |
This support is not native
to PHP4; it necessitates the addition and configuration of an extension.
To have this extension at your disposition on a Linux system, it
is PHP4 must be recompiled by specifying the adequate configuration
options. These extensions are available in the form of objects that have
been precompiled for the Windows environment. These extensions can be
downloaded from the official PHP Web site. You must choose the extension
adapted to the Java version that you use (JDK 1.1.8, 1.2 or 1.3).
In the example, we are using a simple JDK 1.1.8.
In the php.ini
configuration file, you must add two lines in a new “Java” section as in
the following example:
[java]
java.class.path=”c:jdk1.1.8libclasses.zip; C:Program FilesApachecgi-binphp_java.jar; C:Program FilesApachehtdocsphpjava” extension=php_java.dll |
Place the Java classes that you would like to use in
one of the directories given in the list of paths specified by the
java.class.path configuration directive, for example in C:Program
FilesApachehtdocsphpjava
Imagine that you have a Java class named Point, which
represents a point defined by its coordinates and the various
required methods (a constructor, a default constructor, traditional
access methods to set and get proprieties, and a toString method).
The Point class might look something like the following:
public class // public int // default |
Thus, as long as Java support is configured correctly and the
bytecode file Point.class is in the appropriate place, we can
execute the following PHP code:
<?php echo n”;
|
Which triggers the
following display:
Point created at (0,0) …
Point moved to (15,15) (15,15 Type 1) I am a Point 🙂 |
Even more interesting is
that a PointBis class can be derived from a Point class by overloading its
constructors.
public class // super.setType(2); // |
In the same way, we can
instantiate and use an object of the PointBis class as in the following
example:
$myPointBis echo |
Which provides the following results:
Point |
But you might also want to
transfer objects to methods of other objects. For example, let’s take a
class that represents a vector. The vector is defined by its origin (a
point), its standard and its direction. In Java, the Vector class can be
defined in the following manner:
public class // attributes // constructors // Overloaded Object method |
In PHP, we can instantiate
a vector by using the default constructor with the following
code:
$myVector = new Java(“Vector”);
echo “ Vector created, default constructor…”; echo “ “.$myVector->display().” n } |
Which results in the
following display:
Vector created, default constructor…
( (0,0 Type 1) / 0 / 0 ) |
But we can also use the
Vector class constructor to transfer a PHP object
representing a Point object. Consider the example below
// creation n $myVector = n print echo n |
This code triggers the following display:
Point myVector’s |
We can see that the vector
was originally created by its default constructor: It is a null vector for
which the origin is (0,0), having a null standard and a null angle
direction. Then, after calling the setProps() method, we make sure that
the vector origin corresponds to the coordinates of our myPoint point
(11,65).
This simple example is
a way of verifying whether Java can be used from PHP, regardless of if they are
your own Java codes or existing Java classes. This Java gateway has many
applications. Examples include, using CORBA components via IIOP, distributing processes via IIOP, distributing processes via RMI, and coding portions of “sensitive” code
of Java applications so that the source code cannot be unveiled.