Lazy loading is an optimization technique designed to push the 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 define one or more callbacks to execute in order to map a class name to a file.
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
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 '%'
indicates the shell prompt):
% cd path/to/ZendFramework/library % find . -name '*.php' -not -wholename '*/Loader/Autoloader.php' \ -not -wholename '*/Application.php' -print0 | \ xargs -0 sed --regexp-extended --in-place 's/(require_once)/\/\/ \1/g'
This one-liner (broken into two lines for readability) iterates
through each PHP file and tells it to replace each instance of
'require_once' with '// require_once', effectively commenting
out each such statement. (It selectively keeps
require_once() calls within
Zend_Loader_Autoloader, as these classes will fail without
This command could be added to an automated build or release
process trivially, helping boost performance in your production
application. It should be noted, however, that if you use this
technique, you must utilize autoloading;
you can do that from your "
public/index.php" file with the
require_once 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::getInstance();