|
|
@@ -9,9 +9,9 @@
|
|
|
Framework. Between the sheer number of class files that need to be
|
|
|
loaded for many components, to the use of plugins that do not have a 1:1
|
|
|
relationship between their class name and the file system, the various
|
|
|
- calls to <methodname>include_once</methodname> and <methodname>require_once</methodname>
|
|
|
- can be problematic. This chapter intends to provide some concrete solutions to
|
|
|
- these issues.
|
|
|
+ calls to <methodname>include_once()</methodname> and
|
|
|
+ <methodname>require_once()</methodname> can be problematic. This chapter intends to provide
|
|
|
+ some concrete solutions to these issues.
|
|
|
</para>
|
|
|
|
|
|
<sect2 id="performance.classloading.includepath">
|
|
|
@@ -32,16 +32,17 @@
|
|
|
|
|
|
<para>
|
|
|
While this may seem a micro-optimization, the fact is that if
|
|
|
- you don't, you'll get very little benefit from PHP's realpath
|
|
|
+ you don't, you'll get very little benefit from <acronym>PHP</acronym>'s realpath
|
|
|
cache, and as a result, opcode caching will not perform nearly
|
|
|
as you may expect.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
There are two easy ways to ensure this. First, you can hardcode
|
|
|
- the paths in your php.ini, httpd.conf, or .htaccess. Second, you
|
|
|
- can use PHP's <methodname>realpath()</methodname> function when setting your
|
|
|
- include_path:
|
|
|
+ the paths in your <filename>php.ini</filename>, <filename>httpd.conf</filename>,
|
|
|
+ or <filename>.htaccess</filename>. Second, you
|
|
|
+ can use <acronym>PHP</acronym>'s <methodname>realpath()</methodname> function when
|
|
|
+ setting your include_path:
|
|
|
</para>
|
|
|
|
|
|
<programlisting language="php"><![CDATA[
|
|
|
@@ -88,9 +89,9 @@ set_include_path(implode(PATH_SEPARATOR, $paths);
|
|
|
|
|
|
<para>
|
|
|
Another optimization is to combine paths. For instance, Zend
|
|
|
- Framework follows PEAR naming conventions; thus, if you are
|
|
|
- using PEAR libraries (or libraries from another framework or
|
|
|
- component library that follows PEAR CS), try to put all of these
|
|
|
+ Framework follows <acronym>PEAR</acronym> naming conventions; thus, if you are
|
|
|
+ using <acronym>PEAR</acronym> libraries (or libraries from another framework or
|
|
|
+ component library that follows <acronym>PEAR</acronym> CS), try to put all of these
|
|
|
libraries on the same include_path. This can often be achieved
|
|
|
by something as simple as symlinking one or more libraries into
|
|
|
a common directory.
|
|
|
@@ -128,8 +129,8 @@ set_include_path(implode(PATH_SEPARATOR, $paths);
|
|
|
|
|
|
<para>
|
|
|
Let's put all of these suggestions together. Our assumption will
|
|
|
- be that you are using one or more PEAR libraries in conjunction
|
|
|
- with Zend Framework -- perhaps the PHPUnit and Archive_Tar
|
|
|
+ be that you are using one or more <acronym>PEAR</acronym> libraries in conjunction
|
|
|
+ with Zend Framework -- perhaps the PHPUnit and <classname>Archive_Tar</classname>
|
|
|
libraries -- and that you occasionally need to include
|
|
|
files relative to the current file.
|
|
|
</para>
|
|
|
@@ -137,7 +138,7 @@ set_include_path(implode(PATH_SEPARATOR, $paths);
|
|
|
<para>
|
|
|
First, we'll create a library directory in our project. Inside
|
|
|
that directory, we'll symlink our Zend Framework's <filename>library/Zend</filename>
|
|
|
- directory, as well as the necessary directories from our PEAR
|
|
|
+ directory, as well as the necessary directories from our <acronym>PEAR</acronym>
|
|
|
installation:
|
|
|
</para>
|
|
|
|
|
|
@@ -164,7 +165,7 @@ library
|
|
|
<para>
|
|
|
We'll borrow ideas from each of the suggestions above: we'll use
|
|
|
absolute paths, as determined using <methodname>realpath()</methodname>;
|
|
|
- we'll include the Zend Framework include path early; we've
|
|
|
+ we'll include Zend Framework's include path early; we've
|
|
|
already consolidated include_paths; and we'll put the current
|
|
|
directory as the last path. In fact, we're doing really well
|
|
|
here -- we're going to end up with only two paths.
|
|
|
@@ -189,16 +190,16 @@ set_include_path(implode(PATH_SEPARATOR, $paths));
|
|
|
expensive operation of loading a class file until the last possible
|
|
|
moment -- i.e., when instantiating an object of that class, calling
|
|
|
a static class method, or referencing a class constant or static
|
|
|
- property. PHP supports this via autoloading, which allows you to
|
|
|
+ property. <acronym>PHP</acronym> supports this via autoloading, which allows you to
|
|
|
define one or more callbacks to execute in order to map a class name
|
|
|
to a file.
|
|
|
</para>
|
|
|
|
|
|
<para>
|
|
|
However, most benefits you may reap from autoloading are negated if
|
|
|
- your library code is still performing require_once calls -- which is
|
|
|
- precisely the case with Zend Framework. So, the question is: how can
|
|
|
- you eliminate those require_once calls in order to maximize
|
|
|
+ your library code is still performing <methodname>require_once()</methodname> calls --
|
|
|
+ which is precisely the case with Zend Framework. So, the question is: how can
|
|
|
+ you eliminate those <methodname>require_once()</methodname> calls in order to maximize
|
|
|
autoloader performance?
|
|
|
</para>
|
|
|
|
|
|
@@ -206,9 +207,9 @@ set_include_path(implode(PATH_SEPARATOR, $paths));
|
|
|
<title>Strip require_once calls with find and sed</title>
|
|
|
|
|
|
<para>
|
|
|
- An easy way to strip require_once calls is to use the UNIX
|
|
|
- utilities 'find' and 'sed' in conjunction to comment out each
|
|
|
- call. Try executing the following statements (where '%'
|
|
|
+ An easy way to strip <methodname>require_once()</methodname> calls is to use the
|
|
|
+ <acronym>UNIX</acronym> utilities 'find' and 'sed' in conjunction to comment out
|
|
|
+ each call. Try executing the following statements (where '%'
|
|
|
indicates the shell prompt):
|
|
|
</para>
|
|
|
|
|
|
@@ -220,7 +221,7 @@ set_include_path(implode(PATH_SEPARATOR, $paths));
|
|
|
|
|
|
<para>
|
|
|
This one-liner (broken into two lines for readability) iterates
|
|
|
- through each PHP file and tells it to replace each instance of
|
|
|
+ through each <acronym>PHP</acronym> file and tells it to replace each instance of
|
|
|
'require_once' with '// require_once', effectively commenting
|
|
|
out each such statement.
|
|
|
</para>
|
|
|
@@ -315,10 +316,10 @@ Zend_Loader_Autoloader::getInstance();
|
|
|
|
|
|
<para>
|
|
|
Zend Framework 1.7.0 adds an include file cache to the
|
|
|
- PluginLoader. This functionality writes "include_once" calls to
|
|
|
- a file, which you can then include in your bootstrap. While this
|
|
|
- introduces extra include_once calls to your code, it also
|
|
|
- ensures that the PluginLoader returns as early as possible.
|
|
|
+ PluginLoader. This functionality writes "<methodname>include_once()</methodname>"
|
|
|
+ calls to a file, which you can then include in your bootstrap. While this
|
|
|
+ introduces extra <methodname>include_once()</methodname> calls to your code, it
|
|
|
+ also ensures that the PluginLoader returns as early as possible.
|
|
|
</para>
|
|
|
|
|
|
<para>
|