Saturday, November 6, 2010

I'm liking Konstrukt

Wow, my very own blog. And here I am, making my first entry on my Macbook. If only I were in a Starbucks, I could achieve 100% superficial white guy status.

I used to write a blog in my old company, but now that I'm in a new position, I thought it'd be a good time to take the initiative and write a blog of my own. And what better timing than now, since I just so happen to be experimenting with PHP frameworks.

And man, there are a lot of frameworks out there. I came off the Python wagon, where Django was the only framework anyone used. PHP is like a menu at a Chinese restaurant. There are a whole ton of choices, and a lot of them don't make any sense.

PHP Frameworks are no exception. Maybe I'm missing something, but is Zend REALLY worth all the mess? I am admittedly still adjusting to PHP, but Zend seems like a bit of overkill. It is designed for high-end application development, and surely it scales well, but, well, I didn't find it all that elegant. Cake is intuitive, but there seems to be a lot going on behind the scenes that smells like trouble when your site suddenly scales to hundreds of thousands of users.

Here in Japan, a lot of PHP social game developers seem hip to Symfony and Ethna. At first glance, Symfony seems a bit too heavy for my tastes, although I've heard good things about it's performance and scalability. I'm curious to learn more about Ethna, since it's been developed by Japanese SNS "Gree" and is being used pretty widely here in Japan. Unfortunately, all of the documentation is in Japanese, which in itself is too much of an inconvenience for me to bother.

In short, I was looking for a framework that offered simplicity, elegance, and something that I could grasp within 10 minutes of digging into the documentation. Learning some lessons from the financial crisis, I didn't want to take a bet on something I didn't fully understand, just because it was what everybody else was doing.

Fortunately, doing a Google search for "PHP lightweight framework" gave me such a framework: Konstrukt. It is very elegant in it's simplicity. The entire framework API consists of a mere 7 files. No messy installations, configuration is a breeze, and a very low learning curve.

Konstrukt is a Component Oriented Programming (COP) approach to web development. At its core is the idea of heirarchical "components". A component is simply a class, very similar to "controllers" in a typical MVC architechure. They are hierarchical, so if you have a url like http://localhost/foo/bar, the framework will dispatch the request to the "foo" component, which will in turn pass the request to the "bar" component. In the end, a component can display output (like an HTML page), but it doesn't have to. This allows complete encapsulation of the logic in your app, and encourages the developer to naturally establish a clean separation of concerns.

Konstrukt is one of the most lightweight frameworks I've seen for PHP, but it still packs a lot of power. It allows for easy porting with third party tools, like simpletest for unit testing (as Cake also uses). Actually, the tutorial gives step-by-step instructions for how to create and execute functional tests.

It also gives the developer complete control over many parts of the environment, including the structure of your app. Konstrukt uses a "dependency injection container" to allow components to share data without relying on global variables or includes. They recommend using Bucket for your injection controller, and Bucket introduces no additional dependencies (in total, Bucket is a mere 147 lines of code). You could just as easily use a different container, or even write your own.

Using this "di-container" technique, I was able to create my own customizable PDO-like database interface. This gave me complete control over all aspects of the database, such as when the connection is opened or closed. Coming from Django's ORM model, it was really nice to see a framework that offered zero hindrance over how the database is accessed. Best of all, it was fun to do!

I've still got a lot to learn, but I'm really liking Konstrukt's elegance and simplistic approach.

No comments:

Post a Comment