PHP, objects and json

For the last three personal projects I’ve worked on, I needed some kind of way to save objects (let’s say, for example, a review) on the server. After all, there wouldn’t be much use in creating those objects if they were to be deleted as soon as the user looks away.

The most obvious way to do that would have been to use a database, PHP has built-in functions for that. But databases (namely, SQLite and MySql) make me uneasy for three reasons:

Instead, those three projects use flat files, containing json-formatted objects. A movie review, for example, would look something like this:

       "actors": "Jennifer Lawrence, Josh Hutcherson, Liam Hemsworth",
       "director": "Francis Lawrence",
       "title": "Hunger Games: Catching Fire",
       "rating": 4,
       "genre": "Science fiction",
       "mediaType": "movie",
       "uid": 0,
       "lastmod": 1390428994,
       "body": "some description",

It’s relatively easy to read, and almost any modern language can parse this. So each of those three projects has its own functions to handle the conversion between those files and objects. It’s probably not as fast as a database, but it’s good enough for my simple, small projects.

Instead of rewriting the same thing again, I’ve tried to clean up the code a bit, and make it a library, JSONStorage, available on GitHub. The Read Me file and its examples should be detailed enough to understand how to use it.

It work, and I plan to replace the different project’s version with this one, but it’s not perfect by any means. there are probably edge cases I haven’t though of, security problems I haven’t yet picked up. If you find some, don’t go bananas: tell me, I’m eager to do better, and learn along the way. Or, you can fork the project and submit your modifications. In any case, I’ll be pleased to hear from you if you decide to use it.