PHP Tip: How to call a static method on a class where the classname is a variable

I was refactoring some code this week and moved some model meta data from variables into static methods on the classes (so they don’t need to be instantiated to access the meta data). I had a list of the classes in an array and was looping over them, and attempting to call the static method like this:

$test = $foo::bar();

This does not work, you get an “unexpected T_PAAMAYIM_NEKUDOTAYIM” error. This is apparently Hebrew for “two colons”. In other words, PHP doesn’t like you trying to call a static method on a class when the classname is stored in a variable.

Here is the workaround:

$test = call_user_func(array($foo, 'bar'));

Works like a champ.