<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Basic usage</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="java.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java Functions</a></div> <div class="up"><a href="java.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="java.examples-basic" class="section"> <h2 class="title">Basic usage</h2> <p class="para"> <div class="example" id="example-4383"> <p><strong>Example #1 Java Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// get instance of Java class java.lang.System in PHP<br /></span><span style="color: #0000BB">$system </span><span style="color: #007700">= new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.System'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// demonstrate property access<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">'Java version=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$system</span><span style="color: #007700">-></span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.version'</span><span style="color: #007700">) . </span><span style="color: #DD0000">'<br />'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'Java vendor=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$system</span><span style="color: #007700">-></span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.vendor'</span><span style="color: #007700">) . </span><span style="color: #DD0000">'<br />'</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'OS=' </span><span style="color: #007700">. </span><span style="color: #0000BB">$system</span><span style="color: #007700">-></span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.name'</span><span style="color: #007700">) . </span><span style="color: #DD0000">' ' </span><span style="color: #007700">.<br /> </span><span style="color: #0000BB">$system</span><span style="color: #007700">-></span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.version'</span><span style="color: #007700">) . </span><span style="color: #DD0000">' on ' </span><span style="color: #007700">.<br /> </span><span style="color: #0000BB">$system</span><span style="color: #007700">-></span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.arch'</span><span style="color: #007700">) . </span><span style="color: #DD0000">' <br />'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// java.util.Date example<br /></span><span style="color: #0000BB">$formatter </span><span style="color: #007700">= new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.text.SimpleDateFormat'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz"</span><span style="color: #007700">);<br /><br />echo </span><span style="color: #0000BB">$formatter</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.util.Date'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <div class="example" id="example-4384"> <p><strong>Example #2 AWT Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// This example is only intended to be run using the CLI.<br /><br /></span><span style="color: #0000BB">$frame </span><span style="color: #007700">= new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Frame'</span><span style="color: #007700">, </span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$button </span><span style="color: #007700">= new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Button'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Hello Java World!'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-></span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'North'</span><span style="color: #007700">, </span><span style="color: #0000BB">$button</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-></span><span style="color: #0000BB">validate</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-></span><span style="color: #0000BB">pack</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-></span><span style="color: #0000BB">visible </span><span style="color: #007700">= </span><span style="color: #0000BB">True</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$thread </span><span style="color: #007700">= new </span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.Thread'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$thread</span><span style="color: #007700">-></span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10000</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-></span><span style="color: #0000BB">dispose</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> Notes: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> <em>new Java()</em> 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 <em>java.lang.System</em> which expose most of their functionality through static methods. </span> </li> <li class="listitem"> <span class="simpara"> Accessing a member of an instance will first look for bean properties then public fields. In other words, <em>print $date.time</em> will first attempt to be resolved as <em>$date.getTime()</em>, then as <em>$date.time</em>. </span> </li> <li class="listitem"> <span class="simpara"> Both static and instance members can be accessed on an object with the same syntax. Furthermore, if the java object is of type <em>java.lang.Class</em>, then static members of the class (fields and methods) can be accessed. </span> </li> <li class="listitem"> <p class="para"> Exceptions raised result in PHP warnings, and <strong><code>NULL</code></strong> 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: <ul class="itemizedlist"> <li class="listitem"><span class="simpara"> <span class="function"><a href="function.java-last-exception-get.html" class="function">java_last_exception_get()</a></span></span></li> <li class="listitem"><span class="simpara"> <span class="function"><a href="function.java-last-exception-clear.html" class="function">java_last_exception_clear()</a></span></span></li> </ul> </p> </li> <li class="listitem"> <span class="simpara"> 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. </span> <span class="simpara"> Additionally, method names in PHP are not case sensitive, potentially increasing the number of overloads to select from. </span> <span class="simpara"> 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). </span> </li> <li class="listitem"> <span class="simpara"> In the tradition of PHP, arrays and hashtables may pretty much be used interchangeably. 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. </span> </li> </ul> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="java.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java Functions</a></div> <div class="up"><a href="java.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>