PHP
  Home arrow PHP arrow Page 4 - Debugging Program Flow with the Xdebug Extension
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Debugging Program Flow with the Xdebug Extension
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2009-02-23


    Table of Contents:
  • Debugging Program Flow with the Xdebug Extension
  • Review: getting information about PHP objects with the var_dump() function
  • Debugging program flow with the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions
  • Using the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions with a basic class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Debugging Program Flow with the Xdebug Extension - Using the xdebug_start_code_coverage() and xdebug_get_code_coverage() functions with a basic class
    ( Page 4 of 4 )

    In the previous section, I showed you how to use of the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to keep track of which lines are executed by a simple PHP script. Now it's time to demonstrate how these handy functions can be employed within a PHP application that works with a basic class. To do this, I’m going to use the sample “User” class that was listed in the first section of this article, which naturally should be very familiar to you.

    That being explained, here’s the complete signature that corresponds to this sample class:


    class User{

    private $firstName;

    private $lastName;

    private $email;

    public function __construct($firstName,$lastName,$email){

    if(!$firstName){

    throw new Exception('Invalid parameter First Name.');

    }

    if(!$lastName){

    throw new Exception('Invalid parameter Last Name.');

    }

    if(!$email){

    throw new Exception('Invalid parameter Email.');

    }

    $this->firstName=$firstName;

    $this->lastName=$lastName;

    $this->email=$email;

    }

    // get first name

    public function getFirstName(){

    return $this->firstName;

    }

    // get last name

    public function getLastName(){

    return $this->lastName;

    }

    // get email

    public function getEmail(){

    return $this->email;

    }

    }


    Having listed the entire signature of the above “User” class, it's time to see how the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be used conjunctly to display the lines that are executed when some methods of this class are called in a predefined sequence. Here’s the script that performs this process:


    try{

    xdebug_start_code_coverage();

    $user=new User('John','Doe','john@domain.com');

    echo 'First Name :'.$user->getFirstName().'<br />';

    echo 'Last Name :'.$user->getLastName().'<br />';

    echo 'Email :'.$user->getEmail().'<br />';

    var_dump(xdebug_get_code_coverage());

     

    /* displays the following

    First Name :John

    Last Name :Doe

    Email :john@domain.com

    array

    'pathtoexampleflow_debugging_example.php' =>

    array

    8 => int 1

    11 => int 1

    14 => int 1

    17 => int 1

    18 => int 1

    19 => int 1

    20 => int 1

    23 => int 1

    27 => int 1

    31 => int 1

    36 => int 1

    37 => int 1

    38 => int 1

    39 => int 1

    41 => int 1

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    Despite the triviality of the above example, it serves to demonstrate how the “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions can be used for keeping track of which program lines are executed when calling the methods of the previous “User” class.

    Logically, it’s possible (and recommendable, actually) to test these functions with more complex applications, but my intention here is simply to provide you with the right pointers for employing them for debugging the flow of your own PHP programs with more detail.

    Final thoughts

    In this fourth installment of the series, I discussed the usage of the complementary “xdebug_start_code_coverage()” and “xdebug_get_code_coverage()” functions to debug the execution flow of a PHP program. These functions allow you to very simply keep track of which lines are executed by an application.

    This educational journey has not ended yet. Thus, in the next article I will explain how to use this pair of X-debug functions for working with conditional statements.

    Now that you know what the next part will be about, you won’t want to miss it!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek