PHP

Quickstart Guide to Debugging PHP in Sublime Text 3

1. Install Sublime Text 3: http://www.sublimetext.com/3

2. Enable XDEBUG in the PHP.ini file, then restart server.

[XDebug]
zend_extension = "C:\xampp\php\ext\php_xdebug.dll"
xdebug.profiler_append = 0
xdebug.profiler_enable = 0
xdebug.profiler_enable_trigger = 0
xdebug.profiler_output_dir = "C:\xampp\tmp"
xdebug.profiler_output_name = "xdebug_profile.%R::%u"
xdebug.remote_enable = 1
xdebug.remote_handler = "dbgp"
xdebug.remote_host = 127.0.0.1
xdebug.remote_port = 9000
xdebug.trace_output_dir = "C:\xampp\tmp"

3. In Sublime, go to View > Show Console. Run this script to install Package Manager.

import urllib.request,os,hashlib; h = '7183a2d3e96f11eeadd761d777e62404' + 'e330c659d4bb41d3bdf022e94cab3cd0'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

4. In Sublime, go to Preferences > Package Control. Select “Install Package”. Then search for “XDebug Client” and click it to install.

5. Open up your application folder in Sublime (File > Open Folder). Save the folder as a project by going to Project > Save Project As. Save the .sublime-project file in the root of your application folder.

6. Update the .sublime-project configation to look like this, but with your application’s url:

	{
		"folders": [
			{
			"follow_symlinks": true,
			"path": "."
			}
		],
		"settings": {
			"xdebug": {
				"url": "http://my.local.website/",
			}
		}
	}

6b. Sometimes, the XDebug Client doesn’t acknowledge this setting. If this is the case, go to Preferences > Package Settings > Xdebug > Settings – User. Update the “url” setting. You can move it to the top as well for easier access.

7. These are the control keys:

  • Start: CTRL+SHIFT+F9
  • Stop: CTRL+SHIFT+F10
  • Add Breakpoint: CTRL+F8
  • Run: CTRL+SHIFT+F5
  • Step Over: CTRL+SHIFT+F5
  • Step Into: CTRL+SHIFT+F7
  • Step Out: CTRL+SHIFT+F8

8. To use Visual Studio-like keys, add the following Key Bindings configuration (Preferences > Key Bindings – User, before the ending bracket)

    {"keys": ["f9"], "command": "xdebug_breakpoint"},
    {"keys": ["ctrl+alt+f8"], "command": "xdebug_conditional_breakpoint"},
    {"keys": ["f5"], "command": "xdebug_continue", "args": {"command": "run"}},
    {"keys": ["f10"], "command": "xdebug_continue", "args": {"command": "step_over"}},
    {"keys": ["f11"], "command": "xdebug_continue", "args": {"command": "step_into"}},
    {"keys": ["shift+f11"], "command": "xdebug_continue", "args": {"command": "step_out"}},
    {"keys": ["shift+f5"], "command": "xdebug_session_stop"},
    {"keys": ["alt+f5"], "command": "xdebug_session_start"},
    {"keys": ["ctrl+alt+l"], "command": "xdebug_layout", "args": {"keymap" : true}}   
  • Start: ALT+F9
  • Stop: SHIFT+F9
  • Add Breakpoint: F8
  • Run: F5
  • Step Over: F10
  • Step Into: F11
  • Step Out: SHIFT+F11

NOTE: Magento has an issue in its core that causes the debugger to hit a stopping point a number of times before you can actually load your local code. To prevent this, use the following “break_on_exception” config:

    "break_on_exception": [
        // E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR
        "Fatal error",
        // E_RECOVERABLE_ERROR (since PHP 5.2.0)
        "Catchable fatal error",
        // // E_WARNING, E_CORE_WARNING, E_COMPILE_WARNING, E_USER_WARNING
        // "Warning",
        // // E_PARSE
        // "Parse error",
        // // E_NOTICE, E_USER_NOTICE
        // "Notice",
        // // E_STRICT
        // "Strict standards",
        // // E_DEPRECATED, E_USER_DEPRECATED (since PHP 5.3.0)
        // "Deprecated",
        // // 0
        // "Xdebug",
        // // default
        // "Unknown error"
    ],

Leave a Reply

Your email address will not be published. Required fields are marked *