O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Debugging PHP with Xdebug - PHPUK 2018

Debugging in PHP can be tedious and prone to error when using var_dump and echo statements. Learn how to debug more effectively and get better insight into an application by using Xdebug. See how to setup PhpStorm, Xdebug, and the browser to enable debugging that will let you step into code and see variable values live. Extend those debugging capabilities into cli, APIs, and SOAP clients to increase the code that can be debugged with ease.

  • Entre para ver os comentários

Debugging PHP with Xdebug - PHPUK 2018

  1. 1. D E B U G G I N G P H P W I T H X D E B U G M A R K N I E B E R G A L L https://joind.in/talk/50bee
  2. 2. D E B U G G I N G P H P W I T H X D E B U G
  3. 3. A B O U T M A R K N I E B E R G A L L • PHP since 2005 • Masters degree in MIS • Senior Software Engineer • Drug screening project • UPHPU President • CSSLP, SSCP Certified and SME • Drones, fishing, skiing, father, husband
  4. 4. A B O U T M A R K N I E B E R G A L L • Manchester • Nelson • Workington
  5. 5. A B O U T M A R K N I E B E R G A L L
  6. 6. A B O U T M A R K N I E B E R G A L L
  7. 7. D E B U G G I N G P H P W I T H X D E B U G Overview • What is Xdebug • Why use Xdebug • Setting up Xdebug • Using Xdebug
  8. 8. W H AT I S X D E B U G
  9. 9. W H AT I S X D E B U G
  10. 10. W H AT I S X D E B U G Xdebug • Step-into debugger • Profiler • DeBugGer Protocol (DBGp) for communication
  11. 11. W H AT I S X D E B U G Xdebug • Available since 2002 • Developed initially by Derick Rethans • PHP extension • Written in C • Open source
  12. 12. W H Y U S E X D E B U G
  13. 13. W H Y U S E X D E B U G Debugging without Xdebug
  14. 14. W H Y U S E X D E B U G Debugging without Xdebug • echo $something; • var_dump($other); • print_r($data); • error_log(__FILE__ . ‘ line ‘ . __LINE__);
  15. 15. W H Y U S E X D E B U G Debugging without Xdebug 1. Add outputs 2. Load page 3. Look for output 4. Change some code 5. Add more outputs 6. Repeat 7. Cleanup outputs without missing any 8. Load page
  16. 16. W H Y U S E X D E B U G Debugging with Xdebug
  17. 17. W H Y U S E X D E B U G Debugging with Xdebug 1. Turn on debugging 2. Add breakpoint(s) 3. Load page 4. Step into the code
  18. 18. W H Y U S E X D E B U G Debugging with Xdebug • Live values • Follow actual code path • Full stacktrace
  19. 19. W H Y U S E X D E B U G Debugging with Xdebug • Supported by most IDEs • Faster development time • Better understanding of what code is actually doing • Does not require code changes to interrogate values
  20. 20. S E T T I N G U P X D E B U G
  21. 21. S E T T I N G U P X D E B U G Fire up you development environment
  22. 22. S E T T I N G U P X D E B U G Documentation from JetBrain for PhpStorm https://confluence.jetbrains.com/display/PhpStorm/Zero- configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm 1. Install Xdebug 2. Prepare PhpStorm 3. Set a breakpoint in the source code 4. Activate debugger on server 5. Start a debug session in browser 6. Reload the current page 7. Set initial path mappings
  23. 23. S E T T I N G U P X D E B U G 1. Install Xdebug
  24. 24. S E T T I N G U P X D E B U G 1. Install Xdebug • https://xdebug.org/docs/install
  25. 25. S E T T I N G U P X D E B U G 1. Install Xdebug • Download (if applicable) • Install or compile • Configure PHP • Restart webserver
  26. 26. S E T T I N G U P X D E B U G 1. Install Xdebug • git clone https://github.com/xdebug/xdebug.git • cd xdebug • ./rebuild.sh
  27. 27. S E T T I N G U P X D E B U G 1. Install Xdebug • Windows: - Xdebug website has a wizard, dll downloads • Mac: - sudo pecl install xdebug - brew install php72-xdebug • Linux: - wget http://xdebug.org/files/xdebug-2.6.0.tgz - sudo pecl install xdebug
  28. 28. S E T T I N G U P X D E B U G 1. Install Xdebug • Windows: download dll - Xdebug website has a wizard, downloads - Paste php -i or phpinfo() (ex: C:phpphp -i > C: phpphpinfo.txt or <?php phpinfo();) - dll download link provided - Add a line to your php.ini file - Restart webserver
  29. 29. S E T T I N G U P X D E B U G 1. Install Xdebug • apt-get install php-xdebug • wget http://xdebug.org/files/xdebug-2.6.0.tgz
 tar -xzvf xdebug-2.6.0.tgz
 cd xdebug-2.6.0
 phpize
 ./configure --enable-xdebug
 make
 make install
  30. 30. S E T T I N G U P X D E B U G 1. Install Xdebug • yum install php-devel
 yum install php-pear
 pecl install xdebug • dnf install php-devel
 dnf install php-pear
 pecl install xdebug
  31. 31. S E T T I N G U P X D E B U G 1. Install Xdebug • Update php.ini by adding lines:
 
 zend_extension = php_xdebug-2.6.0-7.2-vc15-nts-x86_64.dll
 
 [xdebug]
 xdebug.remote_enable = 1
 xdebug.remote_autostart = 1
 xdebug.remote_port = 9000
 xdebug.remote_host = localhost
 xdebug.idekey = PHPSTORM

  32. 32. S E T T I N G U P X D E B U G 1. Install Xdebug • Update php.ini by adding lines:
 
 zend_extension = /usr/lib64/php/modules/xdebug.so
 
 [xdebug]
 xdebug.remote_enable = 1
 xdebug.remote_autostart = 1
 xdebug.remote_port = 9000
 xdebug.remote_host = localhost
 xdebug.idekey = PHPSTORM

  33. 33. S E T T I N G U P X D E B U G 1. Install Xdebug • Restart webserver - Windows:
 Services
 Apache Monitor - Linux:
 sudo apachectl restart
 sudo service nginx restart
  34. 34. S E T T I N G U P X D E B U G 1. Install Xdebug • Verify via phpinfo - <?php phpinfo(); - php -i | grep xdebug
  35. 35. S E T T I N G U P X D E B U G 1. Install Xdebug • Verify via phpinfo
  36. 36. S E T T I N G U P X D E B U G 1. Install Xdebug • Verify via phpinfo
  37. 37. S E T T I N G U P X D E B U G 2. Prepare PhpStorm
  38. 38. S E T T I N G U P X D E B U G 2. Prepare PhpStorm • Start Listening for PHP Debug Connections
  39. 39. S E T T I N G U P X D E B U G 2. Prepare PhpStorm
  40. 40. S E T T I N G U P X D E B U G 2. Prepare PhpStorm
  41. 41. S E T T I N G U P X D E B U G 2. Prepare PhpStorm • Setup debug settings
  42. 42. S E T T I N G U P X D E B U G 2. Prepare PhpStorm • Setup project path mapping
  43. 43. S E T T I N G U P X D E B U G 2. Prepare PhpStorm
  44. 44. S E T T I N G U P X D E B U G 3. Set a breakpoint in the source code
  45. 45. S E T T I N G U P X D E B U G 3. Set a breakpoint in the source code • Line you know will be hit • Or break on first line
  46. 46. S E T T I N G U P X D E B U G 4. Activate debugger on server
  47. 47. S E T T I N G U P X D E B U G 4. Activate debugger on server • Browser plugin, add-on - The easiest Xdebug (Firefox) - Xdebug Helper (Chrome) - Xdebug Toggler (Safari) - Xdebug launcher (Opera)
  48. 48. S E T T I N G U P X D E B U G 4. Activate debugger on server
  49. 49. S E T T I N G U P X D E B U G 4. Activate debugger on server
  50. 50. S E T T I N G U P X D E B U G 4. Activate debugger on server • URL - GET: &XDEBUG_SESSION_START=PHPSTORM - POST: XDEBUG_SESSION_START=PHPSTORM • Headers - Name: Cookie - Value: XDEBUG_SESSION=PHPSTORM • Bookmarklet
  51. 51. S E T T I N G U P X D E B U G 4. Activate debugger on server
  52. 52. S E T T I N G U P X D E B U G 4. Activate debugger on server
  53. 53. S E T T I N G U P X D E B U G 4. Activate debugger on server
  54. 54. S E T T I N G U P X D E B U G 4. Activate debugger on server
  55. 55. S E T T I N G U P X D E B U G 4. Activate debugger on server
  56. 56. S E T T I N G U P X D E B U G 4. Activate debugger on server
  57. 57. S E T T I N G U P X D E B U G 4. Activate debugger on server
  58. 58. S E T T I N G U P X D E B U G 5. Start a debug session in browser
  59. 59. S E T T I N G U P X D E B U G 5. Start a debug session in browser • Click the debug button in toolbar • Add GET, POST, or COOKIE
  60. 60. S E T T I N G U P X D E B U G 6. Reload the current page
  61. 61. S E T T I N G U P X D E B U G 6. Reload the current page
  62. 62. S E T T I N G U P X D E B U G 7. Set initial path mappings
  63. 63. S E T T I N G U P X D E B U G 7. Set initial path mappings
  64. 64. U S I N G X D E B U G
  65. 65. U S I N G X D E B U G
  66. 66. U S I N G X D E B U G • Breakpoints • Stepping through code • Watches • Console
  67. 67. U S I N G X D E B U G
  68. 68. U S I N G X D E B U G Breakpoints • Pause code execution at specific line • Allowed multiple breakpoints • Conditional breakpoints
  69. 69. U S I N G X D E B U G Breakpoints • Place strategically • Not too early • Not too late • Not too many • Remember time limit, increase if needed • Use conditional breakpoints in loops
  70. 70. U S I N G X D E B U G Breakpoints
  71. 71. U S I N G X D E B U G Breakpoints
  72. 72. U S I N G X D E B U G Stepping through code
  73. 73. U S I N G X D E B U G Stepping through code • Resume Program (Play) • Stop • View Breakpoints • Disable All Breakpoints
  74. 74. U S I N G X D E B U G Stepping through code • Step Over • Step Into • Force Step Into • Step Out • Run to Cursor
  75. 75. U S I N G X D E B U G Stepping through code • Evaluate Expression • Show value addresses • Hide empty superglobal variables (on by default) • Add method to skip list
  76. 76. U S I N G X D E B U G Watches
  77. 77. U S I N G X D E B U G Watches • Live updates • Values • Expressions
  78. 78. U S I N G X D E B U G Console
  79. 79. U S I N G X D E B U G Console • Output from debugging expressions • Enter expressions to be evaluated
  80. 80. S U M M A RY
  81. 81. S U M M A RY • More efficient way to debug • Visibility into code • Ability to change code on the fly • Watch values change line by line • Better understanding of code path
  82. 82. D I S C U S S I O N • Setup issues • Remote debugging • Profiling • Xdebug vs alternative methods
  83. 83. Q U E S T I O N S ? https://joind.in/talk/50bee
  84. 84. S O U R C E S • JetBrains documentation https://confluence.jetbrains.com/display/PhpStorm/Zero- configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm • Xdebug documentation https://xdebug.org/docs/install • David Stockton, php[architect], January 2015, https://www.phparch.com/wp- content/uploads/2015/01/levelup-xdebug-phparchitect-jan2015.pdf • https://sievertschreiber.files.wordpress.com/2010/04/crane-truck-double-fail.jpg

×