Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stop using var_dump(); Embrace step debugging

Stop using var_dump(); Embrace step debugging

EE CONF 2017 Presentation

069965463531f082c67fa8087eff0b05?s=128

BoldMinded

October 09, 2017
Tweet

Transcript

  1. 1

  2. Stop using var_dump(); embrace step debugging 2

  3. Step debugging is stopping code execution on a specific line

    of code, then advancing the application 1 line of code at a time. 3
  4. {exp:channel:entries channel="foo" var_dump="yes" } {entry_id} {/exp:channel:entries} 4

  5. {exp:channel:entries channel="foo"} ={title}={entry_id}={some_custom_field}= {/exp:channel:entries} 5

  6. {exp:channel:entries channel="foo"} <pre>{entry_id}</pre> {/exp:channel:entries} 6

  7. var_dump(); 7

  8. var_dump($entry); die; object(BoldMinded\Publisher\Service\Entry\Entry) private 'request' => object(BoldMinded\Publisher\Service\Request) private 'siteId' =>

    int 1 private 'entryId' => null private 'defaultLanguage' => object(BoldMinded\Publisher\Model\Language) protected 'id' => int 1 protected 'short_name' => string 'en' (length=2) protected 'short_name_segment' => string 'en' (length=2) protected 'long_name' => string 'English' (length=7) protected 'locale' => string 'en_EN' (length=5) protected 'language_pack' => string 'english' (length=7) protected 'cat_url_indicator' => string 'category' (length=8) protected 'is_default' => string 'y' (length=1) protected 'is_enabled' => string 'y' (length=1) protected 'direction' => string 'ltr' (length=3) protected 'sites' => string '[1]' (length=3) protected '_name' => string 'publisher:Language' (length=18) 8
  9. Pros — Easy — No special tools necessary — Works

    anywhere 9
  10. Cons — Slow — Lacks context 10

  11. Alternative function writeToFile($fileName, $data) { $file = PATH_CACHE.$fileName.'.txt'; $stream =

    fopen($file, 'a+'); fwrite($stream, print_r($data, TRUE) ."\n"); fclose($stream); } 11
  12. Xdebug is a PHP extension which provides debugging and profiling

    capabilities. 12
  13. 13

  14. 14

  15. Installing and Configuring apt-get install php-xdebug vim /etc/php/apache2/php.ini zend_extension=xdebug.so xdebug.remote_connect_back=1

    xdebug.remote_autostart=0 xdebug.remote_enable=1 xdebug.show_exception_trace=1 15
  16. 16

  17. Tools — PhpStorm — SublimeText w/ plugin ¯\_(ϑ)_/¯ — Microsoft

    Code w/ plugin 17
  18. Navigating — Setting breakpoints 18

  19. Navigating — Conditional breakpoints 19

  20. Navigating — Stepping over 20

  21. Navigating — Stepping into 21

  22. 22

  23. 23

  24. 24

  25. 25

  26. Tools — https://www.jetbrains.com/phpstorm/marklets/ — Xdebug Helper in Chrome Store —

    ?XDEBUG_SESSION_START=PHPSTORM 26
  27. 27

  28. 28

  29. Demo <blink>Click Here!</blink> 29

  30. Useful breakpoint locations in EE — system/ee/legacy/libraries/Core.php function generate_page() —

    system/ee/EllisLab/Addons/channel/mod.channel.php function entries() — system/ee/legacy/libraries/Template.php function run_template_engine() 30
  31. Personal favorites — Call stack while debugging — Easier to

    debug loops — Can see EE template code at different parse states 31
  32. 32

  33. Remote step debugging zend_extension=xdebug.so xdebug.remote_connect_back=0 xdebug.remote_autostart=0 xdebug.remote_enable=1 33

  34. 34

  35. 35

  36. 36

  37. Demo 37

  38. Hark, there be dragons. 38

  39. Truth You will inevitably lose productivity getting step debug working.

    39
  40. But You will gain that time back 10 fold when

    you're debugging. I promise. 40
  41. Tips Using repeatable scripts to configure environments will make things

    easier. — Ansible — PuPHPet — Docker 41
  42. 42

  43. Questions? 43

  44. Thank You! — @litzinger — @boldminded — boldminded.com 44