Debug in Zend Framework using FirePHP

    Zend Framework and FrePHP

    There is one rule - do not show the debug information to the customer, for this purpose there are usually two configurations, but if the customer is very curious, or do you want to hide kilobytes of debug information for aesthetic reasons? With these good intentions, FirePHP will help us.

    For those who don’t know - FirePHP is an extension of the FireBug plugin for Mozilla Firefox

    For this purpose, we need to modify bootsrap.php in your Zend Framewrok project a bit and add a couple of files to the library. I'll start with a simple one - change bootstrap:
    // paste this piece of code after loading the config file
        // variable from the config that is responsible for outputting debug information
        if ($ config-> application-> debug) {
    / * Turn on all errors and STRICT notices * / 
         error_reporting (E_ALL | E_STRICT);
         ini_set ('display_errors', 1);
         ini_set ('display_startup_errors', 1);
            require_once 'Core / Debug.php'; // connect the class Core_Debug - what it does is read further
            Core_Debug :: setEnabled (true);
            Core_Debug :: getGenerateTime ('Begin');
        } else {    
         / * Turn off all errors * / 
         error_reporting (0);
         ini_set ('display_errors', 0);
         ini_set ('display_startup_errors', 0);

    // this is the inclusion after initialization of the DB adapter
        // something like this its
        $ dbAdapter = Zend_Db :: factory ($ config-> database);
        // connect the profiler
        if ($ config-> application-> debug) {
            require_once 'Core / Db / Profiler.php'; // connect Core_Db_Profiler - this is not a standard profiler for the database
            $ profiler = new Core_Db_Profiler ('Profiler');
            $ profiler-> setEnabled (true);
            $ dbAdapter-> setProfiler ($ profiler);

    Now let's move on to the delicious ...


    Core_Db_Profiler (although by all rules it should be called Core_Db_Profiler_Firebug) is a class for debugging the application’s work with the database (see Zend DB Profiler ), it is inherited from Zend_Db_Profiler_Firebug , it contains only one change - backtrace is added, because It’s very useful to know where this or that database request was made from.

    It looks like this (clickable):


    The Core_Debug class inherits from Zend_Debug and extends its functionality for working with FirePHP, a debug function is added that displays information about the variable (s) in the Fire Bug console, I will give an example of PHP code:

            $ a = $ b = $ c = array (123, 456, 789);
            Core_Debug :: debug ($ a); // output one variable
            Core_Debug :: debug ($ a, $ b, $ c); // print some variables

    The result can be seen in the console: The last line displays backtrace - so that there is no problem finding the line where you call debug. But there is one problem with this debugging method - if there is too much data, then the browser will just freeze, trying to execute JavaScript. There is another useful function in the Core_Debug class - it is called getGenerateTime - it was we who called it right after connecting Core_Debug - the main task of this function is to leave timestamps on the code, i.e. we call the method for the first time - the call time has been saved, we call the second - we save (display) the difference between the previous and first calls. Let me give you the following example, I have three calls in my code:

    // immediately after connecting Core_Debug
    require_once 'Core / Debug.php';
    Core_Debug :: setEnabled (true);
    Core_Debug :: getGenerateTime ('Begin');
    / * ... * /
    // Index Action in Index Controller
    Core_Debug :: getGenerateTime ('IndexAction');
    / * ... * /
    // before dispatching
    Core_Debug :: getGenerateTime ('Before Dispatch');
    Zend_Controller_Front :: getInstance () -> dispatch ();

    The result will again be the FireBug console: Where:


    • Time (sec) - time from the previous “nick”
    • Total (sec) - time from the first "nick"
    • Memory (Kb) - the number of memory eaten from the previous “nick”
    • Total (Kb) - amount of memory eaten for all time

    You can download these classes at the following link: FirePHP Debug (2.5 Kb)

    Related links:

    PS Thanks Baziak 'u for the idea ...

    Link to an article on my blog: Debug in the Zend Framework using FirePHP

    Also popular now: