| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Overview Installation Configuration Quercus SOA/IoC JSP Servlets and Filters Admin (JMX) EJB Amber Security Performance Hessian XML and XSLT Third-party Troubleshooting/FAQ Overview Security Java Integration Resin Module Module Status List of PHP Applications Troubleshooting/FAQ |
Working with Java classes in PHP, Quercus types, and Java to Quercus type mappings. Working with Java classes in PHPImporting classes
Quercus supports the use of an import statement in PHP. <?php import java.util.Date; $a = new Date(123); echo $a->time; ?> Instantiating objects by class nameAn alternative to <?php $a = new Java("java.util.Date", 123); echo $a->time; ?> Calling methodsPHP syntax is used for invoking methods. PHP property syntax can be used for invoking getters and setters of Java objects. <?php import java.util.Date; $a = new Date(123); echo $a->getTime(); # calls getTime() echo $a->setTime(456); # calls setTime(456) echo $a->time; # calls getTime() $a->time = 456; # calls setTime(456) ?> Static members and methodsStatic methods and members are available using PHP syntax if the Java class has been imported. <?php import java.util.Calendar; $calendar = Calendar::getInstance(); var_dump($calendar); ?> An alternative to <?php $class = java_class("java.lang.System"); # System.in $in = $class->in; # System.currentTimeInMillis(); $time = $class->currentTimeInMillis(); ?> import keyword in PHP
Quercus supports the use of an <?php import java.util.Date; $date = new Date(123); ?> User classes can be placed in the webapp's WEB-INF/classes directory. package example; public class MyBean { int _value; public MyBean(int value) { _value = value; } public int getValue() { return _value; } public String makeMessage() { return "Hello, my value is " + _value; } } <?php import example.MyBean; $bean = new MyBean(123); var_dump($bean); var_dump($bean->value); var_dump($bean->makeMessage()); ?> The HttpServletRequest and HttpSession
QuercusServlet automatically creates the
PHP sessions are not shared with servlet sessions. The <?php $session = $request->getSession(true); $foo = $session->getAttribute("foo"); ?> Quercus typesFor every PHP type, there is a Java type that is used
internally to represent the corresponding PHP value. All of the Java types extend
Java method argumentsIn Quercus, Java methods can be called from within PHP. Java arguments for Java methods are marshaled to the correct type from the PHP parameters that were passed in.
When the Java argument type is declared to be Object, the value will be marshaled to a Java object. For example, a PHP int (LongValue) will be marshaled to an Integer. The only exceptions are PHP arrays and objects: they are passed in as-is without marshaling. When the Java argument type is declared to be a Quercus Value, the PHP value is passed in directly without marshaling. If the Java argument type is an object, passing in a PHP Java method returnsWhen a Java method is called from PHP code, the return value of that Java method is marshaled into a valid PHP value.
Java objects like Calendar and Map are placed inside JavaValues and then
returned to the PHP environment. A JavaValue is a wrapper that exposes
the object's Java methods to PHP. For example, if Some Java objects may have an effective PHP value. Take for instance, Date. A Date object is, for practical purposes, a PHP int with it's value pegged to Date.getTime(). Collection, List, and Map behave just like PHP arrays. Suppose
Java method overloadingQuercus allows overloaded Java methods to be called from within PHP code. Quercus will use the method whose arguments are the most easily marshaled (i.e. a PHP string easily goes into a Java String whereas a PHP array is a mismatch for a Java int). import com.caucho.quercus.module.AbstractQuercusModule; public class MyModule extends AbstractQuercusModule { public static void foo(String a, boolean b) { } public static void foo(String a, String b) { } } <?php foo('abc', false); ?> In the example above, the first Java method
|