{"id":714,"date":"2008-07-09T16:48:18","date_gmt":"2008-07-09T15:48:18","guid":{"rendered":"http:\/\/www.itwriting.com\/blog\/714-debugging-php-code-to-fix-a-wordpress-problem.html"},"modified":"2008-07-09T16:48:18","modified_gmt":"2008-07-09T15:48:18","slug":"debugging-php-code-to-fix-a-wordpress-problem","status":"publish","type":"post","link":"https:\/\/www.itwriting.com\/blog\/714-debugging-php-code-to-fix-a-wordpress-problem.html","title":{"rendered":"Debugging PHP code to fix a WordPress problem"},"content":{"rendered":"<p>How do you debug a PHP application? Traditionally developers resort to outputting variable values to HTML, or peering through logs, but why not set breakpoints and step through code just as you would in C# or Java? Maybe because it can take some effort to set this up, as I was reminded today. <\/p>\n<p>I was motivated by an annoying WordPress problem which I&#8217;ve blogged about before. For historical reasons, I have a lot of subscribers to an old RSS url which delivers the feed in the now deprecated RSS 0.92 format. I prefer to have a full text feed, and this used to work fine with WordPress, which placed the entire blog post in the <description>element of the feed.<\/p>\n<p>At some point this stopped working, and subscribers got a summary only. In fact, the feed broke completely for a while, after I switched to pretty permalinks; but even after fixing that, I still had the problem with summary items. I tried upping the length of the description, but it was delivered without any HTML formatting so that did not work.<\/p>\n<p>Next I tried the <a href=\"http:\/\/wordpress.org\/support\/\">WordPress support forums<\/a>. There are lots of good folk there; but if you review the posts its clear that many queries go unanswered. That\u2019s nobody\u2019s fault; it is a community, and for whatever reason there seem to be more people seeking help than there are experts with the time to give free advice.<\/p>\n<p>So how about debugging the PHP code and working out what was happening? It seemed a good opportunity to try the latest <a href=\"http:\/\/www.eclipse.org\/ganymede\">Eclipse Ganymede<\/a>, released a couple of weeks ago, along with the <a href=\"http:\/\/www.eclipse.org\/projects\/project_summary.php?projectid=tools.pdt\">PHP Development Tools<\/a> (PDT). I also figured it would be easier to set this up on Linux, to match what I use on the web server. I used the same Ubuntu on VirtualBox setup that worked well for <a href=\"http:\/\/www.itwriting.com\/blog\/681-installing-sproutcore-on-ubuntu-on-virtualbox.html\">trying out SproutCore<\/a>. It worked&#8230;<\/p>\n<p><a href=\"http:\/\/www.itwriting.com\/images\/php_debugging_full.gif\"><img decoding=\"async\" title=\"Debugging PHP with Eclipse Ganymede\" alt=\"Debugging PHP with Eclipse Ganymede\" src=\"http:\/\/www.itwriting.com\/images\/php_debugging_small.gif\" \/><\/a> <\/p>\n<p>&#8230;but I can\u2019t pretend it was wholly straightforward. Here\u2019s how it went. I installed the latest Ubuntu distro versions of Apache, MySql and PHP \u2013 easy. Ubuntu\u2019s Eclipse is not the latest, so I downloaded it from the <a href=\"http:\/\/www.eclipse.org\">Eclipse site<\/a> and used <a href=\"http:\/\/flurdy.com\/docs\/eclipse\/install.html\">some tips<\/a> to set it up tidily. Note: make sure Sun Java is installed; I set it as the default JVM. Adding the PHP development tools was more fiddly. I\u2019d half expected this to be part of a standard Eclipse download by now, but it is not, and if you try to install it into Ganymede using the standard update site is does not work because of dependency issues (a big problem with Eclipse). You have to download a 2.0.0 build <a href=\"http:\/\/download.eclipse.org\/tools\/pdt\/downloads\/index.php\">from here<\/a> instead.<\/p>\n<p>I\u2019d decided to use the Zend debugger \u2013 that\u2019s a separate Eclipse update too, as explained <a href=\"http:\/\/divby0.blogspot.com\/2007\/08\/pdt-php-debugging-w-zend.html\">here<\/a>. Note that even after updating Eclipse, you still have to install the separate Zend debugger server from <a href=\"http:\/\/downloads.zend.com\/pdt\/server-debugger\/\">here<\/a>, if you want to debug real web applications. I had a few problems getting this working, mainly because of the <strong>zend_debugger.allow_hosts<\/strong> directive which you have to edit in php.ini, and which is not brilliantly documented.<\/p>\n<p>I replicated my blog on the Ubuntu virtual box \u2013 easy. But how do you get your Eclipse PHP project pointing at this existing code? The method I settled on after a couple of experiments was to start a new PHP project, uncheck the Use default option for project contents, and select the blog directory in \/var\/www. You then get a scary dialog which observes that files already exist. You can either create your project as a subdirectory, in which case you cannot debug with the existing files, or else pass the scary warning:<\/p>\n<blockquote>\n<p>Create project in \/var\/www\/blog      <br \/>(Deleting the project will delete the entire \/var\/www\/blog folder)<\/p>\n<\/blockquote>\n<p>I mis-read this at first, thinking it would delete all the files when creating the project. That\u2019s not what it says. Everything was a backup anyway, so I took the plunge; it worked fine. In fact, if you look closely at the screenshot above (click for a full size image) you can see that it is nicely done. You can see the call stack at top left, current variable values, output as it is being generated, and the usual options to step into or over the code.<\/p>\n<p>That said, I did have some problems with Step Into. Just when it was going to be most useful, it bombed out with a message that said Error. If you looked at the detail, that also just said Error. The only fix I found was to set breakpoints in the actual file I needed to debug.<\/p>\n<p>Still, it worked. I found that by adding a single argument to a line in feed-rss.php I could get my full text feed back. I\u2019ve duly <a href=\"http:\/\/wordpress.org\/support\/topic\/187627?replies=2\">reported this<\/a> in the WordPress support forums.<\/p>\n<p>A couple of observations. <\/p>\n<p>First, I don\u2019t much like the WordPress code. Sorry, because the product is marvelous, but the code seems like a typical PHP tangle. Using pretty permalinks, which I regret, makes it worse.<\/p>\n<p>Second, are there not plenty of developers who use both Java and PHP and would like it to be a tiny bit easier to set up in Eclipse? I\u2019m being a little unfair, since Ganymede is just out and I guess the PDT will integrate better with it soon. Even so, Eclipse is still not quite the smooth plug-in dream that I once hoped it would become.<\/p>\n<p>Note that if you don\u2019t mind paying, you can have <a href=\"http:\/\/www.zend.com\/en\/products\/studio\/\">Zend Studio<\/a> which I should think makes life easier. Or perhaps <a href=\"http:\/\/www.codegear.com\/products\/delphi\/php\">Delphi for PHP<\/a>.<\/p>\n<div class=\"wlWriterSmartContent\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9581445b-8be8-47a7-a016-60158f13b6d0\" style=\"padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px\">Technorati tags: <a href=\"http:\/\/technorati.com\/tags\/eclipse\" rel=\"tag\">eclipse<\/a>, <a href=\"http:\/\/technorati.com\/tags\/wordpress\" rel=\"tag\">wordpress<\/a>, <a href=\"http:\/\/technorati.com\/tags\/zend+studio\" rel=\"tag\">zend studio<\/a>, <a href=\"http:\/\/technorati.com\/tags\/zend+debugger\" rel=\"tag\">zend debugger<\/a>, <a href=\"http:\/\/technorati.com\/tags\/ganymede\" rel=\"tag\">ganymede<\/a>, <a href=\"http:\/\/technorati.com\/tags\/ubuntu\" rel=\"tag\">ubuntu<\/a>, <a href=\"http:\/\/technorati.com\/tags\/virtual+box\" rel=\"tag\">virtual box<\/a>, <a href=\"http:\/\/technorati.com\/tags\/delphi+for+php\" rel=\"tag\">delphi for php<\/a>, <a href=\"http:\/\/technorati.com\/tags\/debugging\" rel=\"tag\">debugging<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>How do you debug a PHP application? Traditionally developers resort to outputting variable values to HTML, or peering through logs, but why not set breakpoints and step through code just as you would in C# or Java? Maybe because it can take some effort to set this up, as I was reminded today. I was &hellip; <a href=\"https:\/\/www.itwriting.com\/blog\/714-debugging-php-code-to-fix-a-wordpress-problem.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Debugging PHP code to fix a WordPress problem<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,64,80,92,96],"tags":[],"class_list":["post-714","post","type-post","status-publish","format-standard","hentry","category-linux","category-php","category-software-development","category-virtualization","category-web-authoring"],"_links":{"self":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/714","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/comments?post=714"}],"version-history":[{"count":0,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/posts\/714\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/media?parent=714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/categories?post=714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itwriting.com\/blog\/wp-json\/wp\/v2\/tags?post=714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}