Using JSON on servers without native support

If you’re building a distributed application one of the luxuries you lack is knowing exactly how the server that will be hosting your application is configured. One common obstacle I encounter is lack of JSON support (JSON is only natively available to PHP since version 5 version 5.2, and even then is not uniformly available on servers). Here’s how I code around this situation; firstly, if the server does support JSON, then I don’t want to mess with it, but if it lacks support for JSON, then we need to define the JSON functions so it can use them externally. There are 2 JSON functions that we want to re-create - json_decode(), and json_encode().

Often times these types of things are set using configuration variables (in CodeIgniter, the $config array), but a configuration variable in this case is not really desirable, as a server may get the ability afterwards, or the person implementing it may not set the variable correctly. What I do in order to ensure the system will guess correctly is simply use PHP’s native function_exists() function to determine if the server can handle JSON. If it cannot, then I re-implement all the functions using the PEAR Json_services file, which I implement as a CodeIgniter library.

// Not all servers will have json_decode() available but those that do should
// use it, and we'll fall back to another solution for those who don't. 
if ( ! function_exists('json_decode'))

The library is available from the Pear repository and will work in any environment that CodeIgniter itself works in, simply drop it into your libraries folder.

function codeigniter_controller ()
if ( ! function_exists('json_decode'))




I’ve used this technique successfully now across several CodeIgniter projects.

DOMPDF moved to Google Code, New dev team

When I was building BambooInvoice, I wanted to have the ability to export invoices as PDFs. There are a number of excellent PHP based solutions for this, but Bamboo was unique in that it was a distributed application. I wouldn’t have control over the servers it was being installed onto. This meant I needed a PDF conversion library that didn’t rely on certain server libraries being present. I was also limited in that I wanted BambooInvoice to be released under an Open Source license.

DOMPDF was perfect for what I needed, and today the project changed hands, and is once again being actively developed at I’m very excited for the future of this excellent project.

Read the full post for details.

PHP if statement explained with Robots and James Brown’s Sex Machine

George Ornbo over at ShapeShed (an EE shop, go admire the majesty) wrote a cute introduction to the PHP "if" statment. If you can't have fun with your code... well, then you just plain can't have fun.

I get a fair few requests from designers asking for help with basic PHP. So I'm going to write a series on very basic PHP. It is not hard so let's start with a robot and the if statement.

Not normally worthy of a mention, but he uses robots to explain, and everyone knows I'm a fool for robots; but also offers this masterful bit of PHP code (slightly altered, for brevity).

if ($button == "pressed"{
echo "I am alive and will now perform Sex Machine by James Brown";
else {
echo "Malfunction! Broken, destroyed, smashed.";

Why do I get the sense that this title is going to get me banned from Google?

Things worth reading

I've been kind of in isolation the last week or so, (more on that in another post) and I've not even taken the time to read my RSS feeds latesy, only skimming a few things here and there, or marking things for reading later. So today, I had a bit of time, and boy am I glad I took the time to re-visit those! Here are a few wonderful things worth your time to read.

Man, there was some good stuff on tap! Something from each of those will be making their way into my work in the very near future.

I’m channeling Vanilla Ice to build ExpressionEngine 2

As proof that my eyes are going bleary and that I'm working really hard on ExpressionEngine 2, I present this little gem. Sometime near the end of yesterday, I started to lose my mind. I had been coding for nearly 2 days straight, and had hit setback after setback (I have very little to show for those 2 days unfortunately). An interesting thing happens to me when I'm stressed and tired and frustrated... my mind wanders. It wanders far. In this case, back to 90s rap (keep reading, I swear I'm going somewhere with this). So I did the only logical thing, I decided to drag Derek Jones into the gutter with me and hit him up over IM. He was having a frustrating moment of his own. I was eager to offer a solution:

Me - I have a solution. extend the magic class...
Jones - ::slaps head:: I forgot about it, as per usual.
Me - here's some code

$this->magic->2_legit_2_quit('hey hey'); 

Jones - you didn't...

And it goes downhill from here...

Igniter-zen-i-rails, my new PHP framework

There’s some discussion in a thread on CodeIgniter about microframeworks, and there’s always some discussion about the newest, latest and greatest PHP framework.  I know the market is a bit crowded, but here’s a little something I’ve been working on outside of my time at EllisLab.  I’m a bit hesitant to release it to the world, since it competes directly with both CodeIgniter and ExpressionEngine, but its so good that I can’t help myself.  I hope you’ll enjoy it as much as me.  Here’s some highlights

Seriously, you should try it.

I’ve included my files so far.  Its pretty mature so don’t expect any more development on it.  You can get the files by reading the full post.

CodeIgniter will not be dropping support for PHP 4 anytime soon

There’s been renewed discussion about CodeIgniter moving to a PHP 5 only framework within the community.  This post is my attempt to articulate the point of view of the development team, and my personal reasons for why we will not go this route. 

First of all, let me just say this “I like PHP 5”.  A lot.  I use it exclusively for my personal development, and I want to see it adopted more.  I don’t like PHP 4 much, and in fact, I’d love to not need to support it.  But the fact of the matter is that the vast majority of PHP servers out there are still running PHP 4, and we want our products to be accessible for the widest possible audience.  We will not be dropping support for PHP 4 anytime soon.

What follows in this entry is my full reasoning.  Want to see a PHP 5 CodeIgniter?  Read on…

Quick Link: PHP Advent Calendar

A few days ago I mentioned that was back up for this year.  Well, it seems there’s another good read available!  Chris Shifflet is posting 24 PHP articles from guest authors on his website.  I just got through “Writing Code is Like Doing the Dishes (5 Reasons Why Documenting Your Code Makes You a Better Coder).” by Elizabeth Naramore and I have to say that I really enjoyed it.

So add one more stop to your daily RSS and follow along with the fun.

Judging the work quality of other developers

You ever inherited a site done by someone else, and looked and the site and thought “what the hell where they smoking when they did that, and who gave it to the client making it seem like a good idea”?  C’mon you web-snobs, I know you have.  We’ve all done it.  Heck, I’ve done it recently.

There are two sides to every story

Well, I’m going to stop making any judgments about the quality of other people’s work unless I know the circumstances around how it got developed.  A day of reflecting on my own work has made me rethink things.  Yesterday I was working on a project, plugging away and I thought “hey, I’ve solved this problem before, let me go see what I did”.  So I pull out some old code, and next thing I know, I’m looking over some work I did some time ago.  I wasn’t super impressed. I found a series of nested if statements at one point, when really it could have been written in one line.  Is that the end of the world?  No, but it shows inelegance, and a lack of careful planning.  If this was someone elses work I would have thought “phfft, they’re lazy”.  Anyhow, I was disappointed, so I thought I’d fix it up and upload it for my (former) client….

And then it all came flooding back. 

So as you can see… in a lot of the cases the slightly less elegant code was a result of a changing landscape. I don’t think I could possibly build to quality, when the idea of “quality” is a moving target.  In that case, a nested if just worked for me.  When you’re bound by the triple constraints of cost, quality and speed, then something has to bend.  If the client isn’t willing to sacrifice cost or speed, then quality is going to suffer.

So I’m no longer going to look at

$some_dudes_name 'some name';
"The name of that guy was " $some_dudes_name

and think “why did they even bother”?  Maybe the developer was dumb (that’s always a possibility), or maybe that variable used to hold other information, or maybe they were directed that way, or maybe the dev inherited other code that the client wasn’t willing to let them alter it.  Who knows?

Until I know the history and politics around why something was coded the way it was coded… I’m reserving judgment on anyone else’s work.

Oh yeah, and that inelegant client code I found?  I never did fix it up.

Sanitize your input

A few weeks old, but a great one.  Just found it again today and felt compelled to put it here.  From
comic about data sanitizing