Chris Alexander

On Engineering

Abusing PHP to your whim

Private methods and properties

20th February, 2011

I have recently been writing a lot of code (and open-sourcing it - see here, here, here, here, here, here / here, here, here) and today came across a particular problem and a rather convenient solution provided by PHP’s visibility rules for methods and properties of classes.

I am working on a PHP-based object mapper to help out loading and saving objects to one or more data stores - see Mo. I was adding support for multi-loads, with datastores that can provide multiple results more optimally in a single query (for example, MySQL can select a set of items given a list of IDs). What I wanted to do was:

This is all well and good - the load() method is currently private, and therefore they can’t get to it, but when the constructor calls it there’s no problem. However, if you are doing a multiload and want to return an array of objects, you need to instantiate an object and pass it the data that you have already loaded through your convenient multiload query to the datastore.

Nuts, I thought.

So I looked up PHP’s inheritance rules, and had no luck. But when I got on the visibility section I found a little titbit about PHP that I didn’t know before - an object of a given type may access the private methods and properties of an object of the same type. This is apparently because it already knows the definition of the methods etc. so it can just run them on it - seems a bit like someone was lazy and that was a “feature” that emerged, but we’ll go with that!

This means you can do black magic such as this, which also conveniently solves my problem: