จริงๆก่อนหน้านี้เคยแชร์เรื่องการ debug PHP กันไปแล้ว แต่มาคราวนี้จะมาใช้อีกตัวซึ่งง่ายกว่า นั่นคือการ debug PHP ด้วย dephpugger
1. Install composer
1 2 3 4 |
wget https://getcomposer.org/composer.phar chmod +x composer.phar mv composer.phar /usr/local/bin/composer composer self-update |
2. ติดตั้ง xDebug
1 |
apt-get install php-xdebug |
3. ติดตั้ง dephpugger
1 2 |
composer global require "tacnoman/dephpugger":"dev-master" export PATH=$PATH:$HOME/.composer/vendor/bin |
4. ตรวจสอบว่า dephpugger สามารถทำงานได้ปกติ หรือขาดอะไรหรือไม่
1 |
dephpugger requirements |
5. ทดสอบการ debug ด้วยการสร้าง php file ขึ้นมา จากนั้นที่ส่วนไหนก็แล้วแต่ที่ต้องการ debug ให้ใส่ xdebug_break(); เข้าไปใน code ซึ่งเปรียบเสมือนกับการ break point ใน debugger ใดๆนั่นเอง
1 2 3 4 5 6 7 |
<?php # File index.php $array = [0,2,1,3,4]; xdebug_break(); # <-- Important line. This line is a breakpoint $array = sort($array); echo implode(', ', $array); ?> |
6. เปิด server สำหรับการ debug
1 |
dephpugger server |
ซึ่งจะเกิด Server ที่ทำการรัน php ไปแล้วขึ้นมาที่ port 8888 ซึ่งถ้ามันทำงานปกติ ก็จะแสดงผลของการทำงานปกติขึ้นมา หากเกิด error ใดๆก็จะแสดงขึ้นมา เช่นกัน
7. หากต้องการ debug ใช้เป็น
1 |
dephpugger debug |
แล้วลองเข้า server <debug server>:8888 อีกที ก็จะเห็นที่ฝั่ง console ว่ามีการ debug เกิดขึ้น โดยคำสั่งใน dephpugger มีดังนี้
- n -> To run a step over in code
- s -> To run a step into in code
- set <cmd>:<value> ->Change verboseMode or lineOffset in runtime
- c -> To continue script until found another breakpoint or finish the code
- l -> Show next lines in script
- lp -> Show previous lines in script
- helph -> Show help instructions
- $variable -> Get a value from a variable
- $variable = 33 -> Set a variable
- my_function() -> Call a function
- q -> Exit the debugger
Source:: HackerNoon