Thursday, October 7, 2010

T_PAAMAYIM_NEKUDOTAYIM error message in PHP

I had a trivial task a while ago - to call static method when class name defined dynamically via variable. There were no any need to instantiate class instance to do a simple check if provided class is suitable to work with the current data. So I decided to create a static method that will process a simple check before class instance will be even instantiated.

foreach ($dirDataDrivers as $driverClass) {
    if (true === $driverClass::check($dirPath)) { // ERROR LINE!!!
        // do staff...
    }
}

But my code felt into error message: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM. I've googled around and found that it means "Unexpected double-colon" from Hebrew. You can read about this error in details on PHP documentation website in "Scope Resolution Operator" article.

So the solution for this problem is:
/**
* call static method when class name defined dynamically via variable
*/
$res = call_user_func(  array($driverClass, 'check') , $dirPath );


And finally I'm glad to announce that in PHP 5.3 and further versions this problem will not occur anymore and we can write this code as:

$res = $driverClass::check($dirPath); // As of PHP 5.3.0

Have a nice day!