Avoid complex arrays in PHP

Now that PHP 7+ sorted out a whole bunch of problems with type-hinting of parameters, return values, variables and properties, we turn our attention to somewhat deeper issues.

Array is a native citizen in PHP.  Arrays are very convenient and are widely used.  However, if you stop and think about the times where you had to figure out somebody else’s code, I’m pretty sure complex arrays will come to mind at some point.

I’ve recently came across two completely independent blog posts which talk exactly about this particular area of problems:

Both are explaining the issues very well and make valid points.  As far as solutions and better ways go, apart from the approaches mentioned in these blog posts, I also remembered a recent blog post from which I linked to the data transfer object library, that solves exactly that.

GraphIt – high-performance graph domain specific language

GraphIt is a high-performance graph domain specific language.  If you are involved with graph data structures (web, social networks, maps, and so on and so forth), check it out.  It’s sounds pretty cool.  Here’s a 20 minute video of a talk that does an overview of the language and some examples.

GraphIt is open sourced under MIT license.  Here’s the GitHub repository.

php-jsonq – a simple, elegant PHP package to query over any type of JSON data

php-jsonq provides an easy, yet powerful way to build queries for any JSON data (or PHP data structures for that matter, which are a step away).  This has a variety of useful applications – data migration, API response filtering, complex configurations manipulation, and so on, and so forth.

Working With PHP Arrays in the Right Way

Working With PHP Arrays in the Right Way” is a collection of tips and tricks on the array functions in PHP.  PHP is weird, to say the least, when it comes to arrays, compared to many other programming languages, so articles like that are useful for pointers to better ways of doing things.

I think that even experienced PHP developers will find a bit or two that they either didn’t know or forgot about.

Automerge – a JSON-like data structure for concurrent multi-user editing

Collaborative editing is a very challenging subject, technically speaking.  The old days of users editing a file, sending it to another user, and back are long gone.  Version control tools like git helped with tracking changes and resolving conflicts.  But the newer generation of tools – Google Docs for example – push the bar even higher.  Now many users expect real-time, transparent collaborative tools, which allow multiple users to work on the same document at the same time and without any additional technical complexity.

Automerge is one project that helps the developers to build such collaborative tools, by providing a library of JSON-like data structures, which can be edited in parallel and then merged back together.

Automerge […] supports automatic syncing and merging:

  • You can have a copy of the application state locally on several devices (which may belong to the same user, or to different users). Each user can independently update the application state on their local device, even while offline, and save the state to local disk.(Similar to git, which allows you to edit files and commit changes offline.)
  • When a network connection is available, Automerge figures out which changes need to be synced from one device to another, and brings them into the same state.(Similar to git, which lets you push your own changes, and pull changes from other developers, when you are online.)
  • If the state was concurrently changed on different devices, Automerge automatically merges the changes together cleanly, so that everybody ends up in the same state, and no changes are lost.(Different from git: no merge conflicts to resolve!)