| ||||||||||||||||||||||
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 |
An overview and getting started guide for Quercus.
IntroductionQuercus implements the PHP 5 language in Java, including object
and exception support. Quercus is a mixed interpreted/compiled implementation.
Long-running PHP scripts are compiled to Java for performance, while
short scripts, like Because Quercus is a Java implementation, it natively supports 16-bit
unicode strings and functions. Quercus (in 3.1.0) supports the
new PHP 6 internationalization syntax, and the older unicode conversion
functions like Running an existing PHP application on QuercusIn general, getting an application to run on Quercus is straightforward. To demonstrate this simple process, let's install WordPress. Step 1. Create the Step 2. Create the <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping Curl-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <script-encoding>iso-8859-1</script-encoding> </init> </servlet-mapping> </web-app> Step 3. If you have not already done so, download the
MySQL Connector/J JDBC driver
into Step 4. The configuration for Resin/Quercus is now complete. Now you'll need to go through the application's installation routine. For Wordpress, go to http://localhost:8080/wordpress/wp-admin/setup-config.php to start the installation. That's it for Wordpress on Quercus. If you are encountering issues with a certain application, please see Resources for assistance. For a list of applications running successfully on Quercus, see List of PHP Applications Running on Quercus. Configuring Quercusphp.iniIndividial PHP initialization values can be set in resin-web.xml. For example, to set the settings for sending mail: <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <php-ini> <sendmail_from>my_email_address</sendmail_from> <smtp_username>my_email_username</smtp_username> <smtp_password>my_email_password</smtp_password> </php-ini> </init> </servlet-mapping> </web-app> A PHP style ini file can also be specified: <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <ini-file>WEB-INF/php.ini</ini-file> </init> </servlet-mapping> </web-app> Character EncodingBecause Quercus is 100% Java, Quercus has native support for Unicode and will, by default, parse PHP scripts in UTF-8. This also means Quercus supports the new PHP 6 Unicode keywords like this Unicode typecast: There are three encodings you need to worry about: script-encoding, unicodie.output_encoding, and unicode.runtime_encoding encoding. By default, Quercus uses UTF-8 for all three. Script encoding is for when your scripts are in an encoding other than UTF-8.
Script encoding indicates the encoding that is used in the PHP script source files.
If the source code for an application is not encoded in UTF-8,
the solution is to tell Quercus to parse PHP scripts using the correct character
set (ISO-8859-1 for most applications). For example, to tell Quercus to use
ISO-8859-1, add <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <script-encoding>iso-8859-1</script-encoding> </init> </servlet-mapping> </web-app>
If the PHP application also expects conversion from binary to string using a
character encoding that is not utf-8, then the
<web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <script-encoding>iso-8859-1</script-encoding> <php-ini> <unicode.runtime_encoding>iso-8859-1</unicode.runtime_encoding> </php-ini> </init> </servlet-mapping> </web-app> unicode.output_encoding is the charset used to display output to the browser. You can set it in your resin-web.xml: <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <script-encoding>iso-8859-1</script-encoding> <php-ini> <unicode.output_encoding>iso-8859-1</unicode.output_encoding> <unicode.runtime_encoding>iso-8859-1</unicode.runtime_encoding> </php-ini> </init> </servlet-mapping> </web-app> Compiling PHP Scripts for Increased PerformanceQuercus will automatically compile PHP scripts into Java classes for better performance. This is available only in Resin Professional. The default behaviour in Resin Professional is to execute the PHP script in
interpreted mode, and to compile the script in the background. When the
compiled version is ready, it is used instead of the interpreted version. To
force compilation, use the <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <compile>true</compile> </init> </servlet-mapping> </web-app> Using DatabasesJDBC drivers are required to use databases in Quercus. There are JDBC
drivers for MySQL, Oracle, SQLite, and many other database engines. The
desired JDBC driver should be downloaded into Resin's
The database support in Quercus supports robust database connection pooling since Quercus runs in Resin, a fast Java application server. All PHP database access automatically uses JDBC-pooled connections. PHP code does not need changing to take advantage of this capability. The PHP database apis supported include PDO (portable database objects), mysql, mysql improved, postgres and oracle. Any JDBC-compliant database is available to PHP scripts using PDO. <php $db = new PDO("java:comp/env/jdbc/my-database"); ... ?> JNDI DataSourceIf a database with JNDI name Scripts can use the jndi name directly: <?php // standard PHP //mysql_connect($host, $username, $password, $dbname); // using JNDI lookup mysql_connect("java:comp/env/jdbc/myDatabaseName"); ?>
Or a <web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <database>java:comp/env/jdbc/myDatabaseName</compile> </init> </servlet-mapping> </web-app> Adding PHP functions by creating a Quercus moduleThe core PHP functions are implemented inside Quercus modules. Quercus modules are the Java equivalent of PHP modules. All Quercus modules need to implement AbstractQuercusModule. Functions defined in your modules are callable from within PHP script by using just the function name. Function names need to be distinct in order to prevent name collisions, though Quercus does support function overloading (for Java functions only). A typical Quercus module looks like: package example; import com.caucho.quercus.env.Env; import com.caucho.quercus.module.AbstractQuercusModule; public class HelloModule extends AbstractQuercusModule { /** * @param env provides Quercus environment resources. * @param str */ public void HeLLo_TeST(Env env, String str) { // 'echos' the string env.println("hello " + str); } } <?php // PHP 5 is case-insensitive // just prints "hello me" to the browser. hello_test("me"); ?> For a tutorial on how to implement your own Quercus module, see the Quercus module tutorial. Using Plain Old Java Objects (POJO) in PHP Scripts
You do not need to create a Quercus module in order to use your Java code in a
PHP script. Quercus provides the For more information see Java Integration. <?php // Quercus specific import statement import java.util.Date; $date = new Date(123456789); $list = java("java.util.ArrayList"); ?> Java Function Arguments/Return MarshalingQuercus does marshaling to and from PHP values and Java objects. If a Java function requires a String, Quercus will automatically convert the internal Quercus StringValue to a String. If a Java function returns an For other Java Objects like For more information, see Java Integration. Quercus specific functionsQuercus has several Quercus specific PHP functions for debugging and Java integration (JNDI, JMS, and JMX). For more information, see Resin PHP Functions. Debugging Functions
JNDI
Differences from PHP 6Quercus implements PHP 5 but supports most of PHP 6 language features that are available in the PHP 6 preview distribution. Differences from PHP 6 are:
Module highlightsStandard modulesQuercus implements the standard PHP libraries (arrays, strings, date, regexp, etc). It also supports extension libraries like zip and zlib for compression, mcrypt for encryption, mail (implemented with JavaMail), and bcmath for large numbers. APC (object caching)For PHP object caching, Quercus implements the APC module. PHP applications can use the APC functions to save PHP objects without resorting to serialization and database persistence. Because Quercus runs in Resin, a Java web server, the saved objects are quickly available to any thread running PHP. In other words, unlike Apache which makes sharing across different PHP processes difficult, Quercus can just store a singleton cache of the APC-cached objects. Because Quercus compiles PHP to Java code, PHP scripts get the opcode caching of APC for free. At this time, performance of Quercus is roughtly comparable with performance of mod_php with APC, i.e. it is significantly faster (3-5 times) than mod_php running by itself. Image support ('gd')Quercus provides the image module, so users can use image manipulation functions like watermarking and thumbnail generation in any PHP script on Quercus. .jpg, .png, and .gif files are currently supported. Java users will also find these libraries convenient. PDF generation (PDFlib api)PDF generation in Quercus follows the PDFlib API. Since the Quercus PDF implementation is a new implementation in Java, no special downloads are needed to use PDF. AJAX (JSON)Quercus also includes the JSON module for encoding and decoding AJAX-style requests. The JSON modules is an excellent example of the benefits of writing modules in Java. Because Java supports garbage collection and protects from pointer overruns, the JSON module implementation is straightforward and reliable, without having to worry about all the possible memory problem in a C library. Gettext (localization)Quercus supports the gettext API and .po and .mo files. gettext is a portable API for localization, i.e. translation of program messages. In the future, the Quercus gettext implementation will support Java message bundles so Java applications using PHP can use standard Java localization techniques. Resources
|