Why I still use PHP
Since december 2009, I work in an all Microsoft shop. Coming from a pure LAMP-world – both Python and PHP – the step was quite big to C#, VB and SQL Server. But I’ve grown to really like the technology stack. SQL Server is brilliant much because of SQL Server Management Studio, and I really wish there were a client for MySQL that’s as good as Management Studio. C# is really nice mostly thanks to Visual Studio, which is probably the best IDE there is. And with ASP.NET MVC, Microsoft has a really nice platform for web development. They have borrowed almost all of the best ideas from other successful frameworks like Rails, Django, etc.
But even with ASP.NET MVC, and Django, I still come back to doing web development in PHP in the projects I do on my spare time. And the reason is definitely not language elegance, but it’s very much because of the framework/library Konstrukt.
Most of the other MVC-ish frameworks out there use the idiom of “/controller/action/id/some/other/parameter”, and it just doesn’t feel right. It feels right in simple cases like “/users” and “/users/12″. But what about “/users/edit/12″? The url should – if you ask me – be hierarchical, and it should always be possible to remove the last url segment, and still get a logical response. But if you remove 12 from “/users/edit/12″, what do you get? A bunch of forms, so you can edit all users at the same time?
Instead, Konstrukt handles this by using hierarchical controllers/components. So instead of mapping a request to one specific controller, one controller handles each url segment. So if you have a url like “/users/12/comments”, you’d start of by having the root controller handle the “/” part. That controller sees that the whole url isn’t just “/”, so that controller is responsible for mapping forward to another controller, to handle the “users” segment. That controller also sees that there’s more in the url, and maps forward to a controller that handles “12″, and then – last but not least – the controller that handles the “comments” part. That controller sees that it’s the last part, so it’s responsible for rendering the result. You might think that ends up being an awful lot of controllers, and it sure does, since one controller basically does just one thing. But that’s a good thing. Instead of having a User-controller with Add, Remove, List, Edit-actions. It also lets you reuse controllers very elegantly. You might have both “/comments”, and “/users/12/comments”, for which you can reuse the same controller. Each controller has access to it’s parent controller, so the controller can use it’s parent as a way to filter out comments per user, or display all comments.
I know that I can use routing in most other frameworks to get almost the right result, but it just ends up feeling like I’m wedging it in, it’s not as elegant as with Konstrukt, mostly because of the “one controller with many action methods” thing.
This, together with having control over controller creation which allows you to use a DI container to create them, together with excellent support for different content types, together with the fact that it’s very (almost extremely) loosely coupled, makes me love PHP. I like PHP, but Konstrukt makes me love it. If I start a new web project in Python or C#, I start of with a great feeling of how elegant those languages are, but I always end up really missing PHP because of Konstrukts elegance. If I could just find the time to port Konstrukt to C# and Python…