2007-12-23 20:44:22 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 07:50:41 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-23 20:44:22 +00:00
|
|
|
|
|
|
|
<chapter xml:id="java.examples" xmlns="http://docbook.org/ns/docbook">
|
|
|
|
&reftitle.examples;
|
|
|
|
<section xml:id="java.examples-basic">
|
2011-05-09 14:13:30 +00:00
|
|
|
<title>Basic usage</title>
|
2007-12-23 20:44:22 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Java Example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
// get instance of Java class java.lang.System in PHP
|
|
|
|
$system = new Java('java.lang.System');
|
|
|
|
|
|
|
|
// demonstrate property access
|
|
|
|
echo 'Java version=' . $system->getProperty('java.version') . '<br />';
|
|
|
|
echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
|
|
|
|
echo 'OS=' . $system->getProperty('os.name') . ' ' .
|
|
|
|
$system->getProperty('os.version') . ' on ' .
|
|
|
|
$system->getProperty('os.arch') . ' <br />';
|
|
|
|
|
|
|
|
// java.util.Date example
|
|
|
|
$formatter = new Java('java.text.SimpleDateFormat',
|
|
|
|
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
|
|
|
|
|
|
|
|
echo $formatter->format(new Java('java.util.Date'));
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
<example>
|
|
|
|
<title>AWT Example</title>
|
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2008-03-17 20:35:54 +00:00
|
|
|
// This example is only intended to be run using the CLI.
|
2007-12-23 20:44:22 +00:00
|
|
|
|
|
|
|
$frame = new Java('java.awt.Frame', 'PHP');
|
|
|
|
$button = new Java('java.awt.Button', 'Hello Java World!');
|
|
|
|
|
|
|
|
$frame->add('North', $button);
|
|
|
|
$frame->validate();
|
|
|
|
$frame->pack();
|
|
|
|
$frame->visible = True;
|
|
|
|
|
|
|
|
$thread = new Java('java.lang.Thread');
|
|
|
|
$thread->sleep(10000);
|
|
|
|
|
|
|
|
$frame->dispose();
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
|
|
|
|
Notes:
|
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
<literal>new Java()</literal> 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 <literal>java.lang.System</literal> which expose most of their
|
2008-12-16 03:10:28 +00:00
|
|
|
functionality through static methods.
|
2007-12-23 20:44:22 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Accessing a member of an instance will first look for bean properties
|
|
|
|
then public fields. In other words, <literal>print $date.time</literal>
|
|
|
|
will first attempt to be resolved as <literal>$date.getTime()</literal>,
|
|
|
|
then as <literal>$date.time</literal>.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Both static and instance members can be accessed on an object with
|
|
|
|
the same syntax. Furthermore, if the java object is of type
|
|
|
|
<literal>java.lang.Class</literal>, then static members of the class
|
|
|
|
(fields and methods) can be accessed.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Exceptions raised result in PHP warnings, and &null; 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:
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem><simpara><function>java_last_exception_get</function></simpara></listitem>
|
|
|
|
<listitem><simpara><function>java_last_exception_clear</function></simpara></listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<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.
|
|
|
|
</simpara>
|
|
|
|
<simpara>
|
|
|
|
Additionally, method names in PHP are not case sensitive, potentially
|
|
|
|
increasing the number of overloads to select from.
|
|
|
|
</simpara>
|
|
|
|
<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).
|
|
|
|
<!-- FIXME Why aren't java-doubles converted to PHP-floats? Is this
|
|
|
|
correct? -->
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
In the tradition of PHP, arrays and hashtables may pretty much
|
2008-12-16 03:10:28 +00:00
|
|
|
be used interchangeably. Note that hashtables in PHP may only be
|
2007-12-23 20:44:22 +00:00
|
|
|
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.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
</section>
|
|
|
|
</chapter>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2007-12-23 20:44:22 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|
|
|
|
|