gog's info

Removing require_once() calls from Zend Framework

Goran Jurić,

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.

Posted in Linux,Optimization,PHP,Zend Framework
Tagged with ,

Comments

7 Responses to 'Removing require_once() calls from Zend Framework'

  1. Raymond / 7 Jul 10 at 22:28

    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 / 1 Oct 10 at 22:09

    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. Goran Jurić / 3 Oct 10 at 13:53

    Thank you Tom, I have updated the post with the latest script.

  4. Richard Quadling / 29 Mar 11 at 17:15

    As those commands are non-windows, here is a PHP script which does the same (more or less):
    http://pastebin.com/r0NdvZr7

  5. Faster class loading in Zend Framework application | Irmantas / 11 Apr 11 at 12:35

    [...] One more thing to do, if you did not already did this, is to get rid of require_once statements in Zend Framework class files. If you do not know how to do this there is good tutorial, how to do this in Linux/OS X operating systems: Removing require_once() calls from Zend Framework. [...]

  6. Richard Quadling / 28 May 11 at 03:01

    http://pastebin.com/wHKJZ68e

    0) {
    echo $o_File, ‘ with ‘, $i_Replacements, ‘ replacements.’, PHP_EOL;
    file_put_contents($o_File, $s_Code);
    }
    }
    }

  7. Richard Quadling / 28 May 11 at 03:02

    Oh dear. Can’t post code.

Leave a Reply