Apr 17 2008

I discovered the strange error of “syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM” today. Strange error, it’s been written about before, but to remind myself in future, it means “Double Colon” in Hebrew. The “T” is as normal meaning “Token”.

This also led me to talk to Kelvin about a way about my problem. I had a function something like this (I’ve re-written it as obviously I can’t directly post our company’s source code on the internet!!):

<?php
function SomeFunction($className)
{
  $result = $className::StaticMemberFunction();
}
?>

So if you try this you’ll find that it yields the afformentioned double colon error. It’s not possible to use the :: on a variable name, it must be on a class name. Kelvin told me there’s a couple of solutions, one safe, one unsafe.

The unsafe one is to build PHP code as a string using the get_class_name on $className to statically call StaticMemberFunction(), and eval() that string. eval(), of course is unsafe and absolute care and attention needs to be made to insuring against code injection, something that could be potentially dangerous.

The safer and much easier method is to just instatiate the class. Static functions don’t have to be called statically, it’s just sometimes easier to not have to create an instance of the class just to call a function if it doesn’t access member variables. Unfortunately in this case, where we don’t know what class we’re calling, we have to do it this way, so this will work:

<?php
function SomeFunction($className)
{
  $class_instance = new $className();
  $result = $class_instance->StaticMemberFunction();
}
?>

Yay it works!

One Response to “Paamayim Nekudotayim”

  1. Asgrim » Blog Archive » Namespaces in PHP says:

    [...] there are too many problems using the Paamayim Nekudotayim due to scope, and static classes and whatnot, but I don’t understand why when real [...]

Leave a Reply