Static class awareness coming to PHP 5

From time to time, I go digging through the PHP documentation on PHP.net hoping to discover that the PHP commiters have added better (read: more standardized) support for static properties.  PHP's implementation of object-oriented programming is good, if a little incomplete and inconsistent with other good object-oriented languages (like Java, for instance - no pun intended).

PHP's handling of static properties in one such shortcoming.  Typically, properties and methods defined statically are bound to the class definition, and are accessed using a class' name instead of an instance of it.  For example, a static property named foo of class Bar is accessed Bar::$foo.  Similarly, a static method named world of class Hello is accessed  Hello::world();  PHP incorporates these ideas in a natural fashion.

It is in subclassing that PHP falls all over itself.

Let's say I want to create a static property named foo on a class Bar. The class definition would look something like this

class Bar {
    static $foo = 'value';
}

Now, let's say I want to create a subclass of Bar called BarSubclass.  Typically, if I wanted to provide a new value for the static property foo, I would simply override it in my subclass' definition like this

class BarSubclass extends Bar {
    static $foo = 'new_value';
}

One would then expect the following results

echo Bar::$foo; // "value"
echo BarSubclass::$foo; // "new_value"
echo (Bar::$foo != BarSubclass::$foo); // "1" (for true)

Unfortunately, in PHP land, one gets the following output instead

echo Bar::$foo; // "value"
echo BarSubclass::$foo; // "value"
echo (Bar::$foo != BarSubclass::$foo); // "" (nothing - for false)

The reasoning behind this doesn't really matter to me.  I'm sure it has something to do with PHP's inventors interpreting the word "static" to mean not only "belonging to the class," but also "not overridable."

This evening I received a glimmer of hope.  On February 13 someone updated the PHP docs, and in those docs is a new method: get_called_class().  This method, callable only from within the bodies of class methods, returns the name of the class in which the function is being invoked.  The function can discern the name of the class in which it is being called, even if the method is statically defined.  This means that at long last, I can define static methods that correctly identify the class in which they are being called.

Why is this important?

A great feature for an ORM library (like scottlib) would be a static get method through which callers could load indexed instances of objects by their primary key values, e.g.

$obj = MyObject::get(42);

Until get_called_class(), this was impossible because the definition of the get method existed only at the highest level of the class hierarchy, and there, would be unaware of any subclass that might want to produce an instance of itself instead of the supreme parent.

The only drawback here is that we probably won't be seeing this functionality in our hosting any time soon.  The feature is planned for version 5.3.0.  PHP is presently in version 5.2.8 (my Mac came with version 5.2.6).  In the meantime I'll be adding static loaders and method calls to scottlib, each with an extra parameter for the class name.  Once PHP 5.3.0 hits production, the extra parameter will be phased out in favor of get_called_class().

Hold onto your keyboards - PHP is about to get a whole lot cooler.

About Aaron Collegeman

I started Fat Panda in 2010. I specialize in PHP and JavaScript development, for desktop and mobile applications, and I love WordPress. I'm also the lead developer at Squidoo. You should follow me on Twitter.

, , , ,

Comments