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!

2 comments:

  1. Outstanding post I enjoy your current content; appreciate how we outlined everything, what you are doing an excellent employment lots of other folks that you as a result of which kind of useful weblogs offer attention to be able to people relevant to many points. When i go through other sorts of intriguing blogs out of your internet sites along with I'm a great deal curious along with your blogs expertise, My partner and i in addition did start to write post and this also sort dialogue actually guide me personally out. My partner and i currently saved ones site as well as distributed your current web sites for you to the friends not only me nevertheless every one of them similar to your current running a blog expertise, desire people create much more exciting information sites in this way just one and also all the best . on your future sites.
    Jimmy Wilson-Bourne Legacy Leather Jacket

    ReplyDelete