Well, working with php is colourful at the best of times, but today i encountered a bug so big, so fundamental to the design of the language, that my face almost fell off in disbelief.
In short, if you're calling a method statically in PHP 4, within the static method, the '$this' keyword will point to the calling object, as opposed to being 'null' (there should be no object context in a static method).
Here's an example:
class Static
{
function method()
{
echo get_class($this);
}
}
class Container
{
function Container()
{
Static::method();
}
}
$Container = new Container();
The output is "Container".
This example is lifted off the
bug report page, in which a member of the php team declares, "This is how it is implemented, no bug." . The
php object model documentation backs this up,
The $this pseudo-variable is not usually defined if the method in which it is hosted is called statically. This is not, however, a strict rule: $this is defined if a method is called statically from within another object. In this case, the value of $this is that of the calling object.
What are these guys on? There can be no rational reason for this, the only explanation i can offer up is that this is a side-effect of a deficient object model implementation in PHP 4, passed off as a "feature". Moreover, it means you can't assert that the static method is actually being called statically (remember, there's not even a 'static' keyword in php 4), like so:
assert("!isset($this)");
This will fail since '$this' points to the caller object!
Depressing. Fundamental.