Removing require_once() calls from Zend Framework

If you are using Zend_Loader to load your classes in Zend Framework, there is no need for all the require_once() calls that are littered all over ZF files.

Some acctually report big improvements in responsiveness of their applications and increase in number of transactions per second their hardware can handle because require_once() is quite an expensive operation, especially if the required file is already included prior to the call.

If you are wondering how to comment out all the require_once() calls from your Zend Framework without doing it by hand here is the solution. Navigate to the folder where your installation of ZF resides on the server and just enter this into your shell:

find ./ -type f -exec sed -i 's/require_once/\/\/require_once/' {} \;

My box is running PHP 5.2.6 with APC 3.0.19 and Zend Framework 1.7 PR and my include path is optimized for use with ZF. Stripping require_once() calls didn’t make a drastic impact on my setup and average performance gain was around 3 requests per second (from 63 to 66).

Update (May 16, 2009)

Since the release of Zend Framework 1.8 there is a script to strip requre_once calls from ZF in the ZF Performance guide. This script does not remove require_once calls to the new Zend_Loader_Autoloader component.

% cd path/to/ZendFramework/library
% find . -name '*.php' -not -wholename '*/Loader/Autoloader.php' -print0 | \
xargs -0 sed --regexp-extended --in-place 's/(require_once)/\/\/ \1/g'

Update 2 (October 3, 2010)

Thanks to Tom Anderson for noticing that this does not work since ZF 1.10.8 , the new script for stripping require_once calls is:

% 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'

Note that you must use autoloading if you strip the require_once from the ZF project.

8 thoughts on “Removing require_once() calls from Zend Framework

  1. Raymond

    Hi,
    The snippet just above didn’t work for me:
    -problem with -wholename predicat

    So I used your previous one that fixed it like a charm.

    Thx

  2. Tom Anderson

    With 1.10.8 and other recent releases Zend has changed from Zend_Loader to Zend_Loader_Autoloader and if you’re using Zend_Application to bootstrap your application you must exclude that (/Zend/Application.php) from the comment script listed here in order for your application to work correctly.

    Specifically the problem is line 321:
    if (!class_exists($class, false)) {
    require_once $path;
    if (!class_exists($class, false)) {
    throw new Zend_Application_Exception(‘Bootstrap class not found’);
    }
    }

    This require_once must stay as-is.

  3. Pingback: Faster class loading in Zend Framework application | Irmantas

  4. melkorm

    For those who wants to use it on OSx (dunno if this happens too on other Linux systems):

    find . -name ‘*.php’ -not -wholename ‘*/Loader/Autoloader.php’ \
    -not -wholename ‘*/Application.php’ -print0 | \
    xargs -0 sed -E -i ” ‘s/(require_once)/\/\/ \1/g’

    Had to use -E instead of –regexp-extended and -i ” instead of –in-place.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>