📄 quercus.xtp
字号:
<document> <header> <product>resin</product> <title>Quercus: PHP in Java</title> <level>toplevel-topic</level> <type>contents</type> <description> <p> <em>Quercus</em> is Caucho Technology's fast, open-source, 100% Java implementation of the PHP language. Performance is 4x mod_php and is comparable with PHP accelerator performance. Quercus uses <a href="resin-ioc.xtp">Resin-IoC/WebBeans</a> to integrate with Resin services. </p> </description> </header> <body><localtoc/><s1 title="Introduction to Quercus"><p>Quercus is Caucho Technology's fast, open-source, 100% Java implementationof the PHP language. Quercus is a feature of Caucho Technology's ResinApplication Server and is built into Resin - there is no additionaldownload/install. Developers using Resin can launch PHP projects withouthaving to install the standard PHP interpreter (http://www.php.net) as Quercustakes on the role of the PHP engine.</p><s2 title="What is Quercus"><p>Quercus implements PHP 5 and is internationalization/localization(i18n/l10n) aware. Quercus natively supports Unicode and the new Unicodesyntax of the up-and-coming PHP 6. Quercus implements a growing list of PHPextensions (i.e. APC, iconv, GD, gettext, JSON, MySQL, Oracle, PDF, Postgres,etc.). Many popular PHP applications will run as well as, if not better, thanthe standard PHP interpreter straight out of the box.</p><s3 title="Resin with Quercus"><p>Quercus is much more than just yet another PHP engine. Quercus is the firstto tightly integrate the web server with a PHP engine. Quercus runs on top ofCaucho Technology's Resin Application Server. As a result, PHP applicationscan automatically and immediately take advantage of Resin's advanced featureslike connection pooling, distributed sessions, load balancing, and proxycaching.</p></s3><s3 title="A New Java/PHP Architecture"><p>Quercus is pioneering a new mixed Java/PHP approach to web applications andservices. On Quercus, Java and PHP is tightly integrated with each other - PHPapplications can choose to use Java libraries and technologies like JMS, EJB,SOA frameworks, Hibernate, and Spring. This revolutionary capability is madepossible because 1) PHP code is interpreted/compiled into Java and2) Quercus and its libraries are written entirely in Java.This lets PHP applications and Java libraries to talk directly with oneanother at the program level. Tofacilitate this new Java/PHP architecture, Quercus provides an API and interfaceto expose Java libraries to PHP.</p></s3></s2><s2 title="Benefits of Quercus"><p>Quercus and Quercus' PHP libraries are written entirely in Java, therebytaking the advantages of Java applications and infusing them into PHP. PHPapplications running on Quercus are simply faster, easier to develop, morecapable, more secure, and more scalable than any other PHP solution.</p><p>Quercus gives both Java and PHP developers a fast, safe, and powerfulalternative to the standard PHP intepreter. Developers ambitious enough touse PHP in combination with Java will benefit the most from what Quercus hasto offer.</p><s3 title="Performance - simply faster"><ul><li>Quercus outperforms straight mod_php by about 4x for MediaWiki andDrupal.</li><li>PHP developers can use Java tools like profilers to get in-depthinformation about the PHP program performance.</li></ul></s3><s3 title="Development - fast, safe, and easy"><ul><li>PHP extensions written in Java are fast, safe, and relatively easy todevelop compared to those written in C. Since Java is the library language,developers won't need to be paranoid about third-party libraries havingC-memory problems or segvs and are freed to concentrate on solving theobjectives at hand.</li></ul></s3><s3 title="Capability - powerful Java technologies at the developer's fingertips"><ul><li>Quercus has the best of both worlds: PHP and Java. PHP applications can take advantage of Java technologies like JMS, EJB,SOA frameworks, Hibernate, and Spring.</li></ul></s3><s3 title="Security - no more pesky C memory bugs"><ul><li>All Quercus extensions libraries are coded in Java. Therefore, developersdo not have to worry about C pointer overruns and segmentation faults from PHPextensions anymore.</li></ul></s3><s3 title="Scalability - Massive clusters of PHP"><ul><li>Thanks to Resin, PHP applications can beautifully scale to as many serversas desired.</li><li>PHP applications can now enjoy connection pooling, distributed sessions,fail-safe load balancing, and proxy caching. These benefits require no changein the PHP code.</li></ul></s3><s3 title="Internationalization - 16-bit unicode"><ul><li>Because Quercus is a Java implementation, it natively supports 16-bitunicode strings and functions. Quercus (in 3.1.0) supports thenew PHP 6 internationalization syntax, and the older unicode conversionfunctions like <code>iconv</code>. Since 3.1.3, the new PHP6 unicode featuresare off by default but they can be enabled with the PHP ini<code>unicode.semantics=on</code>.</li></ul></s3></s2></s1><s1 name="existing" title="Existing PHP applications on Quercus"><s2 title="Killer Apps: Mediawiki, Wordpress"><p>Caucho has designated a few applications as Quercus "killer apps".For these applications, we take extra time to test that each new applicationversion works well with Quercus. Any issues raised with the killerapps have priority over other Quercus bugs.</p><ul><li><a href="http://wiki.caucho.com/Quercus:_Mediawiki">http://wiki.caucho.com/Mediawiki</a></li><li><a href="http://wiki.caucho.com/Quercus:_Wordpress">http://wiki.caucho.com/Wordpress</a></li></ul></s2><s2 title="Other applications"><ul><li><a href="http://wiki.caucho.com">http://wiki.caucho.com</a></li><li><a href="http://wiki.caucho.com/Quercus:_DokuWiki">DokuWiki</a></li><li><a href="http://wiki.caucho.com/Quercus:_Drupal">Drupal</a></li><li><a href="http://wiki.caucho.com/Quercus:_Gallery2">Gallery2</a></li><li><a href="http://wiki.caucho.com/Quercus:_Mantis">Mantis</a></li><li><a href="http://wiki.caucho.com/Quercus:_Mediawiki">Mediawiki</a></li><li><a href="http://wiki.caucho.com/Quercus:_Openads">Openads</a></li><li><a href="http://wiki.caucho.com/Quercus:_PHP-Nuke">PHP-Nuke</a></li><li><a href="http://wiki.caucho.com/Quercus:_phpMyAdmin">phpMyAdmin</a></li><li><a href="http://wiki.caucho.com/Quercus:_PHProjekt">PHProjekt</a></li><li><a href="http://wiki.caucho.com/Quercus:_Vanilla">Vanilla</a></li><li><a href="http://wiki.caucho.com/Quercus:_Wordpress">Wordpress</a></li></ul></s2></s1><s1 name="configuring" title="Configuring Quercus"><s2 name="php.ini" title="php.ini"><p>Individial PHP initialization values can be set in resin-web.xml. For example, to set the settings for sending mail:</p><example title="WEB-INF/resin-web.xml"><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></example><p>A PHP style ini file can also be specified:</p><example title="WEB-INF/resin-web.xml"><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></example></s2><s2 name="encoding" title="Character Encoding"><p>Quercus 3.1.0 supports PHP6 and has full support for Unicode. But like PHP6, Quercus 3.1.3 has itsUnicode support turned off by default for compatibility with legacy PHP applications.Unicode support can be enabled with the php ini <code>unicode.semantics</code>.</p><p>With unicode semantics off, Quercus will intrepret bytes in the default ISO-8859-1 encoding.Quercus will behave just as PHP5 would. With it on, PHP5 applications may break andyou need to be concerned with the following three encodings options:script-encoding, unicode.output_encoding, and unicode.runtime_encoding. By default,Quercus uses UTF-8 for all three.</p><p>Script encoding indicates the encoding of PHP script source files.If the source code for an application is not encoded in UTF-8, Quercus may give invalid UTF-8conversions errors when it tries to convert bytes read to UTF-8.The solution is to tell Quercus to parse PHP scripts using the correct characterset (ISO-8859-1 for most applications). For example, to tell Quercus to useISO-8859-1, add <code><script-encoding></code> to the <code>init</code> tagof QuercusServlet:</p><example title="WEB-INF/resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <b><script-encoding>ISO-8859-1</script-encoding></b> </init> </servlet-mapping></web-app></example><p>If the PHP application also expects conversion from binary to string using acharacter encoding that is not UTF-8, then the<code>unicode.runtime_encoding</code> is used to specify the encoding.In PHP 6, there are two types of strings, Unicode and binary. A binarystring is a string where the data is binary, the encoding is unknown, or theencoding is not Unicode (UTF-16). If you ever use a function that will likelyreturn a binary string, then you probably need to set unicode.runtime_encoding.Quercus may convert your binary string to Unicode and then to your outputencoding for output to the browser. If your runtime encoding is wrong, thenyou would see garbage in your browser.</p><example title="WEB-INF/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> <b><unicode.runtime_encoding>iso-8859-1</unicode.runtime_encoding></b> </php-ini> </init> </servlet-mapping></web-app></example><p>unicode.output_encoding is the charset used to display output to the browser.You can set it in your resin-web.xml:</p><example title="WEB-INF/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> <b><unicode.output_encoding>iso-8859-1</unicode.output_encoding></b> <unicode.runtime_encoding>iso-8859-1</unicode.runtime_encoding> </php-ini> </init> </servlet-mapping></web-app></example></s2><s2 name="compiling" title="Compiling PHP Scripts for Increased Performance"><p>Quercus will automatically compile PHP scripts into Java classes for better performance. This is available only in Resin Professional.</p><p>The default behaviour in Resin Professional is to execute the PHP script ininterpreted mode, and to compile the script in the background. When thecompiled version is ready, it is used instead of the interpreted version. Toforce compilation, use the <code><compile></code> tag within the<code><init></code> tag:</p><example title="WEB-INF/resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <b><compile>true</compile></b> </init> </servlet-mapping></web-app></example></s2><s2 name="databases" title="Using Databases"><p>JDBC drivers are required to use databases in Quercus. There are JDBCdrivers for MySQL, Oracle, SQLite, and many other database engines. Thedesired JDBC driver should be downloaded into Resin's <code>${resin.root}/lib</code>directory. Resin will automatically load jars in the <code>lib</code>directory upon startup.</p><deftable><tr> <th>Database type</th> <th>URL for download</th></tr><tr> <td>MySQL</td> <td><a href="http://dev.mysql.com/downloads/connector/j">http://dev.mysql.com/downloads/connector/j</a></td></tr><tr> <td>PostgreSQL</td> <td><a href="http://jdbc.postgresql.org/download.html">http://jdbc.postgresql.org/download.html</a></td></tr><tr> <td>Oracle</td> <td><a href="http://www.oracle.com/technology/software/tech/java/sqlj_jdbc">http://www.oracle.com/technology/software/tech/java/sqlj_jdbc</a></td></tr></deftable><p>The database support in Quercus supports robust database connectionpooling since Quercus runs in Resin, a fast Java application server.All PHP database access automatically uses JDBC-pooled connections. PHPcode does not need changing to take advantage of this capability.</p><p>The PHP database apis supported include PDO (portable database objects),mysql, mysql improved, postgres and oracle. Any JDBC-compliant databaseis available to PHP scripts using PDO.</p><example title="PDO access to JNDI-configured databases"><php$db = new PDO("java:comp/env/jdbc/my-database");...?></example><s3 name="datasource" title="JNDI DataSource"><p>If a database with JNDI name <code>jdbc/myDatabase</code> is defined inresin.xml, (see <a href="config-database.xtp">Database Configuration</a>),Quercus can do a JNDI lookup for the database when database functions arecalled. Thus, database connection parameters like user name can be omittedwithin PHP scripts. This allows easier maintenance and enables Java and PHPdatabase settings to be centrally located in resin.xml.</p><p>Scripts can use the jndi name directly:</p><example><?php // standard PHP //mysql_connect($host, $username, $password, $dbname); // using JNDI lookup mysql_connect("java:comp/env/jdbc/myDatabaseName");?></example><p>You can use a JNDI <code><database</code> configuration in theWEB-INF/resin-web.xml to override the PHP connection code. If a <database>is provided, any <code>mysql_connect</code> call will returnthe configured database,ignoring the parameters to the <code>mysql_connect</code> call.</p><example title="Example: overriding database configuration in resin-web.xml"><web-app xmlns="http://caucho.com/ns/resin"> <database jndi-name="jdbc/mysql"> <driver type="org.gjt.mm.mysql.Driver"> <url>jdbc:mysql://localhost:3306/test</url> <user></user> <password></password> </driver> </database> <servlet-mapping url-pattern="*.php" servlet-class="com.caucho.quercus.servlet.QuercusServlet"> <init> <b><database>java:comp/env/jdbc/myDatabaseName</database></b> </init> </servlet-mapping></web-app></example></s3></s2></s1> <!-- configuration --><s1 title="Java/PHP integration"><s2 title="Using Java services from PHP: Resin-IoC/WebBeans"><p>If you're already using <a href="resin-ioc.xtp">Resin-IoC/WebBeans</a>to organize your application into services, your PHP script can graba reference to the service with the registered name. Calling<code>java_bean</code> with a singleton bean's name will returnthe singleton, and calling <code>java_bean</code> with a component'sname will return a new instance of the component. Quercus will automaticallymarshal PHP calls to the bean's Java methods, so your service's entirefunctionality will be available to the PHP script.</p><example title="Example: using a WebBeans service"><?php $house_manager = java_bean("houseManager"); $house = $house_manager->findHouse("Gryffindor"); foreach ($house->getPrefects() as $prefect) { echo $prefect . "\n"; }?></example><p>WebBeans (JSR-299) is an inversion-of-control/dependency-injectionframework specification for JavaEE 6 which is designed to organizeJava services, and also integrate with scripting frameworks. WebBeansintegrates tightly with the newest EJB and persistence specifications,so PHP applications using the WebBeans interface will gain the latest,cleanest integration with Java applications.<a href="resin-ioc.xtp">Resin-IoC</a> is Caucho's implementation ofthe WebBeans framework, and serves as the underlying architecture of Resinitself, as well as the <a href="resin-ejb.xtp">Resin EJB</a>implementation.</p></s2><s2 title="Working with Java classes in PHP"><s3 title="Instantiating objects by class name"><p>An alternative to <code>import</code> is to use <code>new Java(...)</code> with the class name and anyconstructor arguments.</p><example><?php $a = new Java("java.util.Date", 123); echo $a->time;?></example></s3><s3 title="Importing classes"><p>Quercus supports the use of an import statement in PHP. <code>import</code>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -