PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Getting Better Performance for Plugins

Plugin loading can be an expensive operation. At its heart, it needs to loop through each prefix, then each path on the prefix, until it finds a file that matches -- and which defines the class expected. In cases where the file exists but does not define the class, an error will be added to the PHP error stack, which is also an expensive operation. The question then turns to: how can you keep the flexibility of plugins and also address performance?

Zend_Loader_PluginLoader offers an opt-in feature for just this situation, a class file include cache. When enabled, it will create a file that contains all successful includes which you can then call from your bootstrap. Using this strategy, you can greatly improve the performance of your production servers.

Example 534. Using the PluginLoader class file include cache

To use the class file include cache, simply drop the following code into your bootstrap:

<?php
$classFileIncCache 
APPLICATION_PATH '/../data/pluginLoaderCache.php';
if (
file_exists($classFileIncCache)) {
    include_once 
$classFileIncCache;
}
Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);

Obviously, the path and filename will vary based on your needs. This code should come as early as possible, to ensure that plugin-based components can make use of it.

During development, you may wish to disable the cache. One method for doing so is to use a configuration key for determining whether or not the plugin loader should cache.

<?php
$classFileIncCache 
APPLICATION_PATH '/../data/pluginLoaderCache.php';
if (
file_exists($classFileIncCache)) {
    include_once 
$classFileIncCache;
}
if (
$config->enablePluginLoaderCache) {
    
Zend_Loader_PluginLoader::setIncludeFileCache($classFileIncCache);
}

This technique allows you to keep your modifications to your configuration file rather than code.


Zend Framework