Debugging PHP on a Mac with Netbeans

2

Posted on : 07-11-2009 | By : Brett | In : CodeIgniter, Mac, Netbeans, PHP, Programming

After nearly two months of using OS X, I think I have finally found my groove when it comes to doing my job. That is, writing code for websites, mainly in PHP.

For many years prior to using a Mac for PHP development, I had been using Windows or Linux and Zend Studio 5.x. When I was looking several months back for the Mac version download for my Zend Studio license, I was presented, instead, with a free upgrade to version 6. Wow, that was nice Zend! Unfortunately, the version I paid for (the “perpetual” license) was no longer there to download. But hey! I got the brand-new version. That’s great right? Wrong! Zend decided to drink the Eclipse Kool Aid and completely changed an IDE that I had grown accustomed to (I guess it’s only fair to point out that Zend Studio 5.x was so old at this point that it was getting increasingly more difficult to install on more modern operating systems and newer java platforms.) In spite of the seemingly “over-bloat” of Zend Studio 6.0, I decided to give it a try. I must say that everything just felt wrong. Now I’m sure there are many PHP developers out there that have been using Eclipse for years and can’t live without it, but I just couldn’t seem to make it work my voodoo. I didn’t have the patience to get my Eclipse PHD, so I went looking elsewhere. That led me to a surprising alternative: Netbeans. Although very similar to Eclipse, an IDE with modules/plug-ins for multiple computer language support, Netbeans (entirely free) impressed me right on the spot. I know Zend Studio has all the same features more or less, but the way Netbeans is put together just seemed more natural to me (things I wanted to do actually worked.) Let it suffice to say that I have been using Netbeans since then and I haven’t looked back!

Setting up Debugging with Xdebug and Firefox

In order to leverage the new-found versatility or MAMP, I needed a good debugger. I had had a pretty decent debugger with Zend Studio 5.x and that would be an essential requirement for netbeans. One of the cool features of Zend Studio was the integration of the Zend Toolbar with the debugger. Fortunately, this can be done with netbeans also, but you have to get a firefox plugin called Xdebug Helper. When you get it, note that it has one configuration option you need to set: Xdebug.idekey needs to be set to netbeans-xdebug.

Xdebug Helper for Firefox

Xdebug Helper for Firefox

Since I was using MAMP, I needed to install the xdebug extension for PHP. I actually downloaded the source and built the extension (used Xcode tools to phpize the source and build it and then just copied it to the appropriate MAMP folder.) I wish I had taken the time to record the steps involved, but instead I’ll just refer you to the easy way. Now we just need a little change in our php.ini file and we are good to go:

Note: As of MAMP version 1.8.4, there is an xdebug extension that comes with MAMP (just be sure to disable Zend Optimizer since they don’t seem to play well together.)

; Xdebug config for Mac OS X and NetBeans IDE
zend_extension=/Applications/MAMP/bin/php5/lib/php/extensions/no-debug-non-zts-20050922/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

Xdebug in php.ini

Xdebug in php.ini

Now, all we have to do in order to debug is evoke the debugger in netbeans. I do this by clicking on the debug project button in the netbeans toolbar:

netbeans debug button

netbeans debug button

Netbeans will open up your development website according to the URL you submitted when creating the project (make sure firefox is your default browser and that you have set the default page in your project.) Often, just clicking the button will automatically activate the Xdebug Helper addon for me. But if you want to make sure that netbeans debugs the URL in your browser, be sure the click the X in your firefox footer bar so that it changes color from gray to green.

Xdebug Helper Toogle Icon

Xdebug Helper Toogle Icon

Netbeans will open your startup page to something like:

index.php?XDEBUG_SESSION_START=netbeans-xdebug

If you are using a PHP framework like Codeigniter, the URL can be a little annoying at first. I have found that I only need to press the continue button to let the start page run it’s path and then just close that tab. From that point I just leave the debugger running and toggle the Xdebug Helper “X” icon when I need to debug any page that I am working on. Couldn’t be easier!

Screencast Tutorials

 

Part 1: Prerequites for Xdebug and PHP on a Mac

 

Part 2: Setting up XDebug Helper

 

Part 3: Setting up an virtualhost apache environment in MAMP for codeigniter

 

Part 4: Starting to use XDebug in Netbeans

 

Part 5: Debugging with Netbeans and XDebug

 

Part 6: Wrapping up by showing a few features of debugging with xdebug in Netbeans

PHP: Just for Newbies?

1

Posted on : 03-10-2008 | By : Brett | In : PHP, Programming, Technology

Geeks and Gladiators

2000 years ago, it was Christians. When the Christians became the status quo, it was Jews and Muslims. Since reading recent blogs and after hanging out in a few chat rooms with the “techno-elite”, I’m thinking  the newest group to be “thrown to the lions” are PHP developers.

Why do you ask? Well, just the other day I was hanging out in an obscure chatroom on irc. The channel was the geographically-nearest enthusiast channel for Ubuntu (and I am an Ubuntu enthusiast.) As I was trying to drum up some conversation (mostly because I was bored) I mentioned that I was a PHP developer to stunning silence. Okay?

The next night I log on again, after all, these are supposed to be people that are promoting Ubuntu and usually that encompasses the ability to be friendly and non-condescending. (Please, don’t get me wrong, I feel that, typically, the Ubuntu community is probably the best community for helping everyone fit in.) So, this time no chat is going on (as usual) and I sit idly by until I see some chatter (pretty standard and I was in a few other channels at the moment also.) One of the users (a very prolific and well-known contributor to Ubuntu in my relative geographic domain) was commenting that he had decided to use a certain application. However, he quipped, “Too bad it’s PHP.” Okay, that got my attention. There’s a conversation starter. I write, “Now I see why when I mentioned that I was a PHP developer, the room got really quiet.” Ha ha. I think I got a LOL or two. More silence. I then express how I’m always a little surprised that the django and rails crowd seem to feel some contempt for PHP. Longer silence. Finally, the other fellow posts something along the lines that PHP sucks. Mr. “Ubuntu Loyal” agrees with a two-worder meant more as a textual “high five.” I promptly agree that there is a lot of spaghetti code written by “newbies” in PHP, but that it’s not impossible to write good code using it. To that, I get a very condescending and elitist remark from the other fellow. It was along the lines of, “If you still use PHP, you are still a newbie.” What?! Well, that really ticked me off! I have over 9 years of Web development experience. This means, in my relatively short time in life, I have invested a large portion of my career devoted to writing web software. I consider myself a professional. Who does this elitist punk think he is calling me a “newbie!?” when I express my outrage, Ubuntu-man proceeds to tell me, very non-specifically, why PHP sucks. Basically, he tells me nothing new. He later back-pedals somewhat to convey that it’s the developers preference that really matters. I suppose I’m still a “newbie” by his estimation though. Anyway, I was a bit stunned by this irrational behavior. Was it warranted? Is PHP a truly awful language? Yes, and no.

PHP Complaints

PHP was originally envisioned as more of a templating engine. It began its humble roots in this manner just like many other world-changing web technologies. As more time went on, more feature requests were proposed, and more were implemented, until a “c-like” (block structured, procedural) language emerged. Now, I will take some liberty to make an assumption or two…

Because PHP grew as a collection of functions that are useful for Web development, higher-language programming syntax was not incorporated (Okay, assuming done. Moving on…) One of the chief complaints from the python and ruby-rousers is that PHP has no notion of name space (update. PHP 5.3 and 6.0 now have namespace syntax.) Name space is the practice of enveloping related functions and properties under a common container. The idea is that you can have a save() function in two entirely different name spaces because you can refer to each function with a name-space prefix first. For example, to save a database record, I could call a function named database.save(). That function would not be confused with the whale.save() function by the compiler because “database” and “whale” are different code containers. PHP haters complain that this forces PHP extension makers to create overly-long names and makes PHP less modular (because every single function is in scope.)

Another complaint is that variables are loosely typed (implicit.) That is to say, if I set a variable to “hello world”, PHP automatically assumes a string and converts that variable to a string type. if I set a variable to the numeral 1, PHP assumes I meant an integer…you get the idea. So what’s the problem? Well, without going overboard here, let’s say that my first variable is named $a (PHP variables begin with the dollar sign) and my second varible is named $b. When you write the expression: $a + $b, because one of the variables is of the string type: $a, PHP converts $a to an integer (0) and the result is just “1″. If I change $a to the literal number 2, PHP now makes an integer assumption and adds the two integers to make the result: 3. Critics of PHP complain that implicit typing creates more opportunities for bugs.

I’m just going to bunch together a few of the most common complaints that remain. PHP has no default caching mechanism. PHP uses anonymous arrays, PHP only has UNIX dates (1970-2038.) There are many other “band-wagon” and “brain-stormed” reasons. Many of which are deservedly so. Admittedly, It’s not a perfect language, but does it really hold you back so effectively as to engender contempt?

So yes, PHP has it’s limitations. Those of us who embrace PHP and use it on a daily basis are aware of PHP’s short-comings. But, just like any other language with “snags” (no programming language is perfect) we have learned to get around them to get the job done. I cannot say at this point that any of the issues complained about has created any significant roadblocks for me. In fact, a significant number of popular free open source web software out there is written in–you guessed it–PHP! If it’s so bad, why do these developers not switch to python or ruby? A few examples are drupal and wordpress (this blog is powered by drupal wordpress.) So, according to the “rail-heads” and “django-divas” we are led to believe that PHP programmers, such as the coders for drupal and wordpress are basically ignorant and coding with one brain-lobe tied behind their backs!

Irrational and unwarranted nonsense!

It’s almost to the point of paranoia. It reminds me of the Apple ads trying to convince you that everybody hates Vista. So, don’t use it or you’ll be sorry! As much as I would love a mac, I actually have Vista, and I like it! Developers with a few years of good PHP programming behind them actually like it as well!

Again, I promise that clean, easily-refactorable, and intuitive code can be, and is, written in PHP! For example, name space really isn’t the issue that the rhetoric suggests. I mean, really, how much harder is it to write mysql_connect() as opposed to mysql.connect()? (yes, it’d just be connect() if name space were declared but still not that big of an issue.) You can emulate name spaces by writing PHP’s code into your own objects which can be written very clear and concise just like any other high-level language. It all depends on the developer. Certainly not a “show stopper.” Furthermore, PHP 5 fixes a lot of the issues that have been criticized in the past. For example, I mentioned implicit types. PHP 5 supports type hinting. The difference is that you can choose to use it. If you have something simple you’re working on, you can choose not too. Some people actually consider implicit types as a convenience. I think it’s very convenient to be able to choose. In the several years I have been developing with PHP, rarely has implicit typing been an issue. Maybe the critics are just lazy programmers that need the compiler to babysit them. On that note, I recently read a post from a developer who stated that one of the many reason he hated PHP so bad was the lack of op caching. Even though it’s not built into the core of PHP, there is a free caching extension called APC. The manual gives you easy steps on how to install it. Again, you have the choice to use it or not. And speaking of extensions, the new PDO database abstraction extension is simply awesome!

I have heard a famous purveyor of a popular web framework rail on PHP citing that “his way” is more natural for web development. What he was really saying is that his framework that is built on another language is easier and faster than developing PHP. Unfortunately, he gives the impression that his preferred language is better for Web development than PHP. In reality, he is comparing a framework for rapid web development to a language. Not a fair comparison. While, I agree that the MVC model for web development works well, I think he is being disingenuous in his notion that his framework is the only way to accomplish this “natural way.”  For example, there are numerous PHP frameworks that allow you to use the MVC model of development (my favorite is codeigniter.)

No Language is perfect. Even so, PHP is a great choice for beginners and pros alike.

I do admit that, because PHP doesn’t enforce a model for development (you can write procedural or object oriented code), programmers with little experience can write pretty awful and almost impossible-to-maintain code. But hey, you can write crappy code in any language!

PHP was designed to be a convenient language to learn and just knock stuff out (email forms, website authentication, etc.) And it performs this amazingly well!  Web applications are more frequently simple than not. I have never felt held back by what I could or could not perform using PHP. I can also note, from a perspective of enterprise, PHP scales up just fine. The large variety of Web applications available that are written in PHP is proof. It all comes down to how smartly they were built. Facebook works just fine folks! Yes, it’s true, compiled code will always outperform code compiled on-the-fly, but python and ruby aren’t compiled either (besides, compiled code makes web development a pain in the backside IMHO.)

So yes, PHP may not be a perfect and high-level language. But then again, you don’t need a nail gun to build a bird house, a hammer works just fine. Don’t believe the hype! PHP works amazingly well for what it was meant to do. I wear a badge of pride that I call myself a PHP developer, so should you. Stand up for yourself!

Ruby on Rails is cool

2

Posted on : 12-08-2006 | By : Brett | In : Programming, Technology

Being the PHP aficionado I am, I tend sometimes to stay in my little PHP bubble (ASP made me distrust & dispise the Web scripting world.) Every now and then I come out and see something that is a breath of fresh air.

Ruby on Rails (or ROR) is yet another framework for Web applications. It’s not kludgy like Java and .NET though. What I mean (or meant) is that the learning curve is moderate as opposed to being quite daunting to plunge into like other frameworks for Web.

Ruby is a relatively new language on the block, but is gaining popularity in the development world for its simple and clean syntax and portability.

Ruby on Rails is easy to setup on windows so you can play with it yourself before committing to turn it up on your live site. O’Reilly OnLamp.com has an excellent tutorial to get you up and running with a web editable database in minutes!

I certainly plan on giving it a whirl within the next few months.