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

die() hard @ phpse

die() hard @ phpse

There are several tools out there for debugging PHP applications, but many developers still prefer cluttering their code with var_dump() and die(). After finding out the reasons for that, we will have a look at the Xdebug extension and it’s relatively new challenger phpdbg.

Avatar for Sebastian Heuer

Sebastian Heuer

October 19, 2015
Tweet

More Decks by Sebastian Heuer

Other Decks in Technology

Transcript

  1. <?phpeople ?> “Debugging is like being the detective in a

    crime movie where you are also the murderer.” – Filipe Fortes “Crime Scene” by Alan Cleaver, CC BY 2.0, https://www.flickr.com/photos/alancleaver/4121423119
  2. <?phpeople ?> CONTROL INSPECTION MODIFICATION 1 <?php 2 3 class

    SomeWorker 4 { 5 6 /** @var SessionInterface */ 7 private $session; 8 9 /** @var SessionInterface $session */ 10 public function __construct(SessionInterface $session) 11 { 12 $this->session = $session; 13 } 14 15 public function run() 16 { 17 die('reached SomeWorker::run()'); 18 $this->session->put('foo'); 19 $this->session->put('bar'); 20 } 21 22 }
  3. <?phpeople ?> 1 <?php 2 3 class SomeWorker 4 {

    5 6 /** @var SessionInterface */ 7 private $session; 8 9 /** @var SessionInterface $session */ 10 public function __construct(SessionInterface $session) 11 { 12 $this->session = $session; 13 } 14 15 public function run() 16 { 17 var_dump($this->session); 18 fwrite(STDERR, var_export($this->session, true)); 19 $this->session->put('foo'); 20 $this->session->put('bar'); 21 } 22 23 } CONTROL INSPECTION MODIFICATION
  4. <?phpeople ?> 1 <?php 2 3 class SomeWorker 4 {

    5 6 /** @var SessionInterface */ 7 private $session; 8 9 /** @var SessionInterface $session */ 10 public function __construct(SessionInterface $session) 11 { 12 /* Using a stubbed session for debugging purposes 13 * @todo Remove before commit! 14 $this->session = $session; 15 */ 16 $this->session = new Session(); 17 $this->session->setUsername('[email protected]'); 18 } 19 20 public function run() 21 { 22 $this->session->put('foo'); 23 $this->session->put('bar'); 24 } 25 26 } CONTROL INSPECTION MODIFICATION
  5. <?phpeople ?> code coverage $ phpunit PHPUnit 4.7.3 by Sebastian

    Bergmann and contributors. Runtime: PHP 5.5.9-1ubuntu4.9 with Xdebug 2.2.3 Configuration: /vagrant/dev/datapool/phpunit.xml.dist ............................................................... 63 / 286 ( 22%) ............................................................... 126 / 286 ( 44%) ............................................................... 189 / 286 ( 66%) ............................................................... 252 / 286 ( 88%) .................................. Time: 7.44 seconds, Memory: 31.50Mb OK (286 tests, 581 assertions) Generating code coverage report in Clover XML format ... done Generating code coverage report in HTML format ... done
  6. <?phpeople ?> code coverage in PHPUnit $ phpunit PHPUnit 4.7.3

    by Sebastian Bergmann and contributors. Runtime: PHP 5.5.9-1ubuntu4.9 Configuration: /vagrant/dev/datapool/phpunit.xml.dist ............................................................... 63 / 286 ( 22%) ............................................................... 126 / 286 ( 44%) ............................................................... 189 / 286 ( 66%) ............................................................... 252 / 286 ( 88%) .................................. Time: 7.44 seconds, Memory: 31.50Mb OK (286 tests, 581 assertions) with Xdebug 2.2.3 Generating code coverage report in Clover XML format ... done Generating code coverage report in HTML format ... done
  7. <?phpeople ?> code coverage in PHPUnit $ phpunit PHPUnit 4.7.3

    by Sebastian Bergmann and contributors. Runtime: PHP 5.5.9-1ubuntu4.9 Configuration: /vagrant/dev/datapool/phpunit.xml.dist Warning: The Xdebug extension is not loaded No code coverage will be generated. ............................................................... 63 / 286 ( 22%) ............................................................... 126 / 286 ( 44%) ............................................................... 189 / 286 ( 66%) ............................................................... 252 / 286 ( 88%) .................................. Time: 7.44 seconds, Memory: 31.50Mb OK (286 tests, 581 assertions)
  8. <?phpeople ?> XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_JMP); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_JMPZ); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_JMPZ_EX); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_JMPNZ); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_IDENTICAL); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_NOT_IDENTICAL); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_EQUAL); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_NOT_EQUAL);

    XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_SMALLER); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_IS_SMALLER_OR_EQUAL); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_BOOL_NOT); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_ADD); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_SUB); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_MUL); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_DIV); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_ADD_ARRAY_ELEMENT); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_RETURN); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_RETURN_BY_REF); XDEBUG_SET_OPCODE_OVERRIDE_COMMON(ZEND_EXT_STMT); opcode overloading https://github.com/xdebug/xdebug/blob/master/xdebug.c
  9. <?phpeople ?> no fixed remote IP needed since Xdebug 2.1

    Animation taken from http://www.xdebug.org/docs/remote