Naturally, before you start using the X-debug library, it’s necessary to install it on your own testing web server. To do so, you should navigate to its official web site, located at http://x-debug.org, and then proceed to download either the Windows or Linux module. For Windows, it’s provided as a .DLL file. In this case, I’ll be using the library that works with PHP 5.2.1 – 5.2.6 for Windows. Once you’ve downloaded the correct file, place it in any directory of your choice (mine is “ext” under the PHP folder) and finally add the following line to your php.ini file within the “dynamic modules” section: zend_extension_ts="c:/php/ext/php_xdebug-4.4.1-2.0.2.dll" Please make sure that the above line includes the full path to the X-debug extension, so it can be found by the PHP engine. Assuming that the X-debug PHP extension has been installed correctly on the web server, it’s time to test it and see if it’s really working as expected. To do this, I’m going to build a basic sample class, called “FileReader,” that looks like this: class FileReader{ private $path; public function __construct($path){ $this->path=$path; } public function read(){ if(!$content=file_get_contents($this->path)){ throw new Exception('Error reading target file.'); } return $contents; } } As shown above, the previous “FileReader” class is useful for fetching the contents of a selected file, which can be directly echoed to the browser via its “read()” method. So far, there is nothing interesting happening here, right? This is about to change. Now I’m going to create a simple script that uses this class and passes into it an invalid path for the file to be read. Here it is: try{ $fileReader=new fileReader('invalid_path/data.txt'); echo 'Contents of target file are the following .'.$fileReader->read(); } catch(Exception $e){ echo $e->getMessage(); exit(); } If all goes well (or wrong, to be more precise), as soon as the previous script is executed, the X-debug library will swing into action, and display the following error message:
At this point, you’ll have to agree with me that things are getting pretty exciting now. As shown before, the X-debug extension provides plenty of information about the error raised by the previous script, informing you not only about the nature of the error itself, but also about a few other useful debugging parameters, including the stack of calls that has been made during the script’s execution, the time it took to process each of these calls and the memory used, and finally the function and the location of each stack entry. So at this point, you can see that the X-debug extension is up and running in the web server, and naturally it can be used for debugging PHP applications by means of additional data that isn’t available by default with the PHP interpreter. So far, so good. At this stage I've demonstrated, through an introductory hands-on example, how to perform a basic test of the capabilities offered by the X-debug extension for debugging PHP applications in a detailed way. However, this is only the beginning of this hopefully educational journey. The X-debug library comes equipped with many other handy features that deserve a close analysis. Thus, it’s time to continue exploring them in depth. In the section to come, I’ll be showing you how to use the library’s “xdebug_call_file()” function to keep track of the location of the files included and executed by a PHP application. So, please click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|