JSON PHP Library Composer Package

I’ve started working with Laravel 4 and am really enjoying the power of this PHP framework.

Now that Laravel uses Composer packages, managing libraries and dependencies has become very simple – it solves so many of the problems I’ve struggled with in recent years when managing the components and libraries for the applications I build.

I have built a few libraries over the years to help me in my development projects, so as I’ve started developing with Laravel, I’ve chosen to re-write these as Composer packages. I’ve also taken the opportunity to re-architect my code and make it more testable and extensible.

Further to this, I decided that I would publish my libraries publicly for the first time so that other people could use them if they choose. I think my code is sound enough now to stand up to some public scrutiny!

To enable this, I migrated to using Bitbucket to host my Git repositories – which allows me to have both private and public repositories and is free for small teams.

Previously I was using CodebaseHQ, which is good, but I think their pricing model is a bit backwards. With Codebase, you pay based on the number of projects, not based on the number of users. This means that even if you work alone, but create a large number of projects, you end up paying a lot more than is reasonable for a single developer. I largely got around this by using my three allowed projects largely just as groupings for my collection of Git repositories. It was still costing me £5 per month, which is why migrating to Bitbucket is a no brainer really.

I created an account on Packagist to host the Composer packages, which links in with Bitbucket. Packagist is just an index, Composer uses it to identify where to download the code from, which in my case will be my Bitbucket Git repositories. For those packages I want to keep private for internal use only, I have set up a Satis repository on one of my servers which links with my private Bitbucket Git repositories.

I also finally spent the time to learn how to write unit tests using PHPUnit – I’m not sure why I didn’t do this a long time ago, it really helps with code writing and identifying problematic code before it becomes a difficult bug!

My first published effort is a simple JSON library which wraps the existing PHP json_encode and json_decode functions.

The main reason for building this library was to provide additional error handling to show more meaningful error messages than those given by json_last_error. I always found myself writing the same code for error handling when decoding JSON, so I figured it would work better in a reusable library.

Of course, the meaningful error message problem should hopefully be fixed in PHP 5.5 with the new json_last_error_msg function – but I’m still mostly developing for PHP v5.3 and have only just upgraded my main server to PHP v5.4. Even then, I think a simple wrapper can still make the functions easier to work with.

When I get a chance, I’ll post some documentation for my libraries on this site.

Until then, there is the Git repository on Bitbucket: Hampel Json and the Packagist package: hampel/json

It would be nice to get some kind of indication as to who (if anyone) is using your package, I suspect the 10 installs currently shown for my Json package are all me!

Tags: ; ; ; ; ;

  • Share it!

Leave a Reply

Your email address will not be published. Required fields are marked *