PHP Insanity V
Mark Mzyk | February 29, 2008
Today’s insanity comes from PHP’s brain dead implementation of object orientation.
PHP has the concept of static functions. This is certainly good, because it means you don’t always have to instantiate an object to get access to a function. However, there is also the bad.
PHP allows you to call a function statically that isn’t static.
If you try to call a non-static function statically, any language should convulse violently and throw massive errors.
PHP just opts to take it silently. Perhaps it threw a warning in the background, but this should fatal out, not just warn.
It gets better. You have managed to call the non-static function statically. That function has a $this->somefunction() call within it. PHP, in its infinite wisdom, will use the value of $this that was last loaded, so it uses the last instantiated object, likely the object you just called the non-static function statically from. When PHP doesn’t find somefunction() in the object it has determined that $this refers to, it will finally convulse and throw a fatal. However, if the object that PHP determines $this references happens to have a function called somefunction(), it is possible PHP matches on it and attempts to execute the function. What happens at this point is anyone’s guess.
Can we all just make a pact to abandon PHP so it will fade into the ether? The world might just become a better place.