PHP CLI remote debugging with PhpStorm & Zend Debugger

This post aims to give an overview of how to get remote cli debugging working with PhpStorm and Zend Debugger. You can find more detailed information under the links in this article.

Some time ago i wanted to debug a PHPUnit test in PhpStorm, debugging PHP scripts using the cli sapi turned out to require some extra effort.

The situation is that my project files reside in a virtualbox, and open the project files -via a mounted directory- in PhpStorm. This means that, for normal web debugging, you have to specify some server settings, including the path mapping.

Since you work on the server directly, you have to set up a remote debugging connection to your IDE. In his blog post, Kevin Schroeder explains how to do this.
You can do this by running the following command, as copied from Kevin’s post:

Don’t forget to change the IP-address and port to the address that you’re IDE is running on.

Now there is one more thing you have to do to make PhpStorm understand where the files are, otherwise you still wouldn’t hit those breakpoints.
According to this article from the JetBrains team, this can be done by setting the PHP_IDE_CONFIG environment variable to “serverName=name-of-server” where name-of-server is the name as configured in Project Settings -> PHP -> Servers.

And oh, make sure your “Listen to Debug connections” button is green!

As i work with different projects on the same virtualbox, i have created this small script that you can put in your .bashrc

You can enable debugging by running:

$ bugon server-name

And disable by running:

$ bugoff

Happy debugging!

Anticipating for change with the Strategy-pattern

We developers get to deal with change nearly every day. As your project progresses, stories or requirements are changed or added. And we’ve all been to the point where our code gets brittle or unmaintainable after too many changes and/or additions.

Let’s start with a metaphor. Let’s say your keyboard is broken and you need a new one. Luckily the keyboard is connected via a USB-connector to your computer, so you can replace it with another keyboard with ease.
But what if there wasn’t a common interface (USB) to connect your keyboard to a computer, for example if it was directly connected to the computer?
Indeed, you would either get an entire new computer or you’d be soldering your new keyboard onto your computer. None of which are ideal solutions to the problem.

This metaphor applies to programming in many ways, but arguably the most important principle that comes from this example is that you should program to an interface where possible.

In a lot of cases programming to an interface encourages the interchangeability of certain parts of your application. Thus it enables you to write flexible code, and anticipate for changes in domain logic.

If you’ve ever written or worked with an online shop you might have come a cross the problem that different countries have different tax rates. Each country has their own tax rate, which of course can change independently. When implementing an online shop, this problem cries out for a flexible solution, and you need to think of a durable strategy to solve this problem.

Diagram 1 - Different implementations of TaxInterface

A good way to do this is to look for commonalities of the different calculations. It is not hard to see that every tax calculation has nothing more than an in- and output number. As long as the tax is calculated correctly, the calculation itself is not really important to the system.
These commonalities are the things you define in the interface, in this case we’ll call the method calculateTax. By programming each country-specific implementation to the interface (TaxInterface) the Order does not need to know (or worry) about the outcome of TaxInterface::calculateTax(). The only thing that it needs to do is instantiate the approperiate implementation, which is out of the scope of this post.

See Diagram 1 for a clarification.

This solution is defined as the “Strategy pattern”, and it is about anticipating on (potential) change of features. In the example it is fairly easy to add a tax implementation for another country, while you have to make only small changes to your existing code.

I hope this example demonstrated the reason behind programming to an interface and and the purpose of the Strategy-pattern in a fairly simple manner.

My progress on the PHP on Azure project

Windows Azure LogoIt’s been a while since i’ve blogged about the PHP on Azure contest project. This post will be about the project, but not necessarily about my implementation of PHP on Azure.

As i wrote in my introductory post about the project, the main reason to participate in the contest is to put a lot of things i recently learned into practice. And so far so good.
My main focus is not to build a good application in general, but to me it is about discovering and experimenting on how to keep a PHP application and all that comes with it (search engine, multi media converter, database) as scalable as possible.

Before starting on the project i highly underestimated what is involved with building a highly scalable solution. It definitely involves more than just deploying a project on the Windows Azure platform.

This is also the main reason i haven’t blogged about PHP on Windows Azure that much yet. Since my main goal is to deploy everything on the Windows Azure platform (database, files, search engine and the application itself of course) i had a lot of stuff to look up and experiment, as i’ve never worded with IIS7 and MSSQL in combination with PHP before, leave alone with Windows Azure (or any other cloud platform).

Nevertheless, i really enjoy working on this project. As the deadline is about 1.5 months away from now, a lot of work can be done still.
More or less i feel i am at a point where i can focus my attention on building the application instead of looking up and experimenting on things (as much fun as it is, though).

In the (near) future i’ll write up some tips and tricks here on my blog, hoping to catch up on the bi-weekly blogging requirement for the contest ;-)
There’s a lot to share, i just haven’t put anything into practice yet, so it’s hard to blog about.

By the way, the other participants have shared some really interesting things about their project. You can read all their experiences on the PHP on Azure contest page.

PHP on Azure contest

Lately i have been learning a lot of new things like development techniques, best practices and design patterns. Now obviously the best way to make all this information persist in my head is to bring it into practice immediately!

To start doing this i’ve started contributing to open source software projects like the Zend Framework. And besides helping to improve the quality of the framework framework you’ll  automatically become part of a large worldwide community of developers. To talk and share ideas with developers all over the world is an experience on itself and very inspiring too.

So, about Windows Azure and the contest

Now two weeks ago i was at the PHPbenelux conference where Maarten Balliauw announced the PHP on Azure contest. At first, this contest didn’t really get my attention because i wasn’t aware of what all the cloud computing ‘hype’ is all about in the first place.

After reading about Windows Azure and the concept of cloud computing in general i was surprised by it’s simplicity. It sounds like something very complicated, but in fact, from a developer perspective it is not.

From my perception running applications on Windows Azure, or cloud platforms in general is a developers’ playground full of possibilities, how awesome is that?!
How close to reality my perception will be has yet to be seen though, because i’m participating on the contest!

About the project

The project i’ll be working on for this contest is one that i’ve been playing with in my mind for a while now. It is an online (collaborative) documentation publishing platform.
Now this is actually a pretty simple concept. It’s a platform where people can collaborate on an information collection. Whether it be an online book, reference guide, manual or what other type of information you want to present online.
The added value of when presenting information on the web is that you can add something very imporant to the content: metadata. This metadata can be anything, really. You can add or embed pictures, video’s, files and links. Or, to improve interaction even more it is possible to start discussions or share the information on social platforms.

Now you might associate this concept with Wikipedia, but in fact it is nothing like it.
I’ll list some of the features of my concept, and you’ll see why it is not to be compared with Wikipedia:

  • Each information collection (or project) is presented seperately. They are all seperate projects, like with books.
  • It is possible to use a traditional publishing workflow: Writer > Reviewer > Editor
  • Content version control might be implemented
  • The look-and-feel of every individual information collection (or project) can be altered. Consider each project a seperate website.

It is just an idea, yet

For now, all of the above are just ideas. During the development my idea of the concept might change and i have to see what is possible to develop before the contest deadline.
However to me this project is a great way to put all the new interesting things that i’ve learned in the past months into practice. And for some reason, the project being an entry for a contest makes it easier to motivate myself ;-)

I think the project is a perfect fit for a cloud platform like Azure. Because with applications that *could* have a lot of users, an unknown amount of content (both textual and binary) it is really awesome that you do not have to worry about scaling options, but just focus on the application itself: It actually makes designing the application easier from the ground up!

Development platform and software

One of the first things that really caught my attention about Windows Azure, is that it comes with it’s own local development environment. You can use this environment by installing the Windows Azure SDK. I will use this environment, along with IIS7, SQL Server Express 2008 R2 and PHP 5.3 to develop the application. As you might expect, the framework i’ll use for developing this project will be the Zend Framework.

I think this setup is the best way to deploy my project on the Windows Azure platform, and to make full use of all the components it offers.

Are you in?

So that describes my project for the contest. If you’re still reading this blog post it looks like you might be interested in taking a shot at the PHP on Azure contest as well.
If so, i would like to get in contact! Let’s share idea’s, approaches and practices about Windows Azure or PHP apps in the cloud in general. Post a link to your blog in the comments or find me on Twitter!

If you want to start right away, you can find more information about the contest, and you can start right away with these Windows Azure introductionary account offers.

Over the next couple of weeks i’ll be writing some more (technical) blogposts about the PHP on Azure contest. So if you’re interested, take a regular look on my weblog, subscribe to the RSS feed or follow me on Twitter!

Using a PHP IDE: The start of a journey

So this week i’ve started the epic journey of setting up an environment to make full use of an integrated development environment for PHP.

Now using version control, develop locally, debugging, auto code-completion etc. is something i’ve heard a lot of people talk about lately.
Not because it’s a hot-topic, but because it’s mandatory for every (PHP) developer that wants to make full use of his or hers creative mind. Yes that’s right, what i considered first to be “Overhead” like version control seems to be mandatory for making your life as a developer easier.

As my day to day job requires me to work with several developers directly on a FTP server, i was never able to use a PHP IDE to it’s full extent. Things like debugging, auto code completion, version merging do not seem to be possible with *any* PHP IDE when you work directly onto a FTP server. So this has to change. And besides, this leads to a lot of problems which i probably don’t even have to explain.

Now that i am becoming more interested in Open Source development i have to do some research on how to set this all up.
Which is exactly what i am going to do this weekend.

There are a couple of problems i ran into before even starting to set things up. I still have to think of a solution to these:

  • Database version control
  • Automatic deployment
  • Merging feature branches to the testing/staging/live branches

Probably how to handle database version control concerns me the most, have to read up on that a bit more.

PS: Any idea’s, tips or tricks are welcome, as i am fairly new to all of this!