A.3. Autoloading ZF and Zym

A.3. Autoloading ZF and Zym

Switching from the ZF and Zym default of requires to autoloading is fairly simple but it requires that you keep a separate custom copy of the libraries. Running the script below on the library files will remove all references to requires allowing you to use autoloading as you choose.

<?php
$directories = array(
    '../library/Zend',
    '../library/Zym'
);
 
foreach ($directories as $directory) {
    $iterator = new RecursiveDirectoryIterator($directory);
 
    foreach(new RecursiveIteratorIterator($iterator) as $file) {
        if ($file->isFile() &amp;&amp; substr($file, -4) == '.php') {
            $contents = file_get_contents($file);
            $contents = preg_replace('/[\t\r]*require_once\s+?[(\'"].+?;[\t\r]*/six', '', $contents);
            $fh = fopen($file, 'w');
            fwrite($fh, $contents);
            fclose($fh);
        }
    }
}