Ajax... JSON or Partial HTML?

Single Page web applications can be a good way to enhance a user’s experience. When developing single page web apps, what content type should you return in your responses? Recently, I came across this advice: “Never, and I mean NEVER return marked-up data using AJAX (use JSON)”

For a long time, I have agreed with this assertion. It makes sense right? JSON is small, HTML is big. But, is that all there is to it? Will a user really notice the difference?

I created a simple demonstration. Open it using firefox or chrome and view the net tab in either firebug or developer tools. You can view the difference by submitting the form with either json or html selected. Sure enough, html is bigger (514b v 145b). And, json is faster (1.47s vs 1.52s).

But, can you really notice any difference? I can not. And unless your application is going to be serving tens of thousands of requests each day or your responses are very large (which they generally aren’t), your users won’t notice either.

There is another consideration too. If you use json, you then have to write more javascript to map the json data to your forms or other views. If you use partial html, there is no mapping needed.

Posted on 05 January 2013. Comments

Auto loading php classes take two

My previous post describing auto loading classes in php, is getting a bit out-dated. Since then, some folks have gotten together to help standardize class loading resulting in PSR-0. Here is their sample implementation:

It works quite well and can work with the old-style psuedo namespaces too. Here is an example of how to use it:

If you are still using php’s __autoload function or some other method of autoloading classes, I highly recommend switching to a PSR-0 compliant autoloader.

Posted on 10 January 2012. Comments

PHP Traits Example

The upcoming release of php 5.4 introduces traits.
This example adds a getObjectId method to a class:

Posted on 20 November 2011. Comments