Example and speed advantages
Hi, I said I'd show an example and show how merging files together can increase speed, here I go.
First of all, here's my directory tree :
admin/
/doc/
/image/
/include/
/lib/
/lib/Cookie.php
/lib/Db.php
/lib/DbMysql.php
/lib/...
/setup/
/setup/compile.php
1. Edit the compile.php file's attribute $file, $dir and constant ROOT
PHP Code:
define('ROOT', '../');
define('LINEBREAK', "\n");
define('BRACKET_ON_SAME_LINE', 1);
/**
* "Compiler" Class. This class reads all files from a directory and merge them
* into a big one. Very usefull to get some speed if, in each pages, you
* include a lot of differents files.
*
* @author Jean-Philippe Déry <jeanphilippe_dery@hotmail.com>
* @version 1.0
*/
class Compiler
{
/**
* Compile each files from this directory.
*
* @var string
* @access private
* @since 1.0
*/
var $dir = 'lib/';
/**
* Compiled file.
*
* @var string
* @access private
* @since 1.0
*/
var $file = 'include/all.php';
/* .... */
First, I define my ROOT value. Since compile.php is in /setup, the relative root would be one directory behind so ../ If you would like to compile all files in /home/username/www/test/lib/ and your compile.php is in /var/www/php/compile.php then your relative root would be ../../../
Now you must set the $dir attribute. This attribute is the directory where all your lib files are. The path must be from your root dir and must end and a / like lib/ for the first case and home/username/www/test/lib/ for the second case
Finally set the path (from your root) where the all-in-one file will be written with the $file attribute.
If this ROOT thing confuse you, simply leave it blank and set a full path to both $dir and $file attribute.
2. Execute the script. You should see something like this (but with your files)
Code:
Compile file Cookie.php ............ done
Compile file Db.php ....................... done
Compile file DbMysql.php ............. done
Compile file File.php .................. done
Compile file Form.php ............................ done
Compile file FormFactory.php ......... done
Compile file FormOutput.php ................................. done
Compile file FormValidator.php ...................... done
Compile file Header.php ....... done
Compile file Layout.php .................... done
Compile file Mail.php .. done
Compile file Module.php ............. done
Compile file ModuleAdmin.php .............. done
Compile file Navbot.php ..... done
Compile file Navleft.php ...... done
Compile file NavLeftAdmin.php .... done
Compile file Navtop.php ..... done
Compile file Navtree.php ........ done
Compile file Output.php .............. done
Compile file PgOutput.php ......................... done
Compile file PgOutputAdmin.php ....................... done
Compile file ResultSet.php ........... done
Compile file ResultSetMysql.php ......... done
Compile file Session.php .................. done
Compile file Std.php ........... done
Compile file System.php ........................... done
Compile file Template.php ................................. done
Compile file TemplateFactory.php ............. done
Compile file TemplateFileFactory.php .............. done
Compile file User.php ........................... done
Compile file Widget.php ................................................................................. done
Compile file Zipfile.php ................................... done
Compilation succeeded
Now the test :
The script was timed this way :
PHP Code:
$stimer = explode( ' ', microtime() );
$stimer = $stimer[1] + $stimer[0];
// Time this...
$etimer = explode( ' ', microtime() );
$etimer = $etimer[1] + $etimer[0];
echo '<p style="margin:auto; text-align:center">';
printf( "Script timer: <b>%f</b> seconds.", ($etimer-$stimer) );
echo '</p>'
;
Every pages has been timed 3 times
Results :
Without using the compiled class :
Script timer: 0.193107 seconds.
Script timer: 0.143650 seconds.
Script timer: 0.137079 seconds
Using the compiled class :
Script timer: 0.083128 seconds.
Script timer: 0.079790 seconds.
Script timer: 0.082525 seconds.
Here's where I include the compiled file:
PHP Code:
<?php
// +---------------------------------------------------------------------------+
// | Framework for Php4 |
// |---------------------------------------------------------------------------+
// | Author : Jean-Philippe Déry <jeanphilippe_dery@hotmail.com> |
// +---------------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation; either version 2 of the License, or |
// | (at your option) any later version. |
// +---------------------------------------------------------------------------+
// Définit le niveau d'affichage d'erreur
error_reporting(E_ALL);
// Constantes & configurations
require_once ROOT . 'include/constants.php';
// Charge le fichier contenant toute les classes compilée si il existe sinon
// on inclus tous les fichier requis un par un.
if (is_readable(ROOT . 'include/all.php'))
{
require_once ROOT . 'include/all.php';
}
else
{
// Libraries
require_once ROOT . 'lib/Cookie.php';
require_once ROOT . 'lib/Db.php';
require_once ROOT . 'lib/DbMysql.php';
require_once ROOT . 'lib/File.php';
require_once ROOT . 'lib/Module.php';
require_once ROOT . 'lib/ModuleAdmin.php';
require_once ROOT . 'lib/ResultSet.php';
require_once ROOT . 'lib/ResultSetMysql.php';
require_once ROOT . 'lib/Session.php';
require_once ROOT . 'lib/Std.php';
require_once ROOT . 'lib/System.php';
require_once ROOT . 'lib/Template.php';
require_once ROOT . 'lib/TemplateFactory.php';
require_once ROOT . 'lib/TemplateFileFactory.php';
require_once ROOT . 'lib/User.php';
// Output
require_once ROOT . 'lib/PgOutput.php';
require_once ROOT . 'lib/PgOutputAdmin.php';
require_once ROOT . 'lib/Output.php';
require_once ROOT . 'lib/Header.php';
require_once ROOT . 'lib/Layout.php';
require_once ROOT . 'lib/Navleft.php';
require_once ROOT . 'lib/NavleftAdmin.php';
require_once ROOT . 'lib/Navtop.php';
require_once ROOT . 'lib/Navtree.php';
require_once ROOT . 'lib/Navbot.php';
// Formulaires
require_once ROOT . 'lib/FormFactory.php';
require_once ROOT . 'lib/FormValidator.php';
require_once ROOT . 'lib/Form.php';
require_once ROOT . 'lib/FormOutput.php';
require_once ROOT . 'lib/Widget.php';
}
?>
Thanks for reading