I am experiencing something in PHP that seems very odd. I am trying to access a static property from a non static method. I need to use the static keyword to access this property as it can have different values in each child class.
However, instead of accessing a property from the expected class at all, it instead accesses the property from the calling class. This seems like a bug to me, but if it isn't, I was wondering if someone could explain this behaviour to me and also explain how I can access this static property.
My expectation here is that the static property $why would be taken from class B. I am perplexed as to why instead it would be taken from Class A.
<?php
error_reporting(E_ALL & ~E_STRICT);
class A
{
public static $why = "Really don't want this value. Bug?";
public function callB()
{
$B = new B;
$B::getWhy(); // PHP Bug?
$B->getWhy();
$B::getWhyStatic();
$B::getWhyStaticSelf();
}
}
class Base {
protected static $why = "Don't want this value";
public static function getWhyStatic()
{
echo static::$why . "<BR>\n";
}
public static function getWhyStaticSelf()
{
echo self::$why . "<BR>\n";
}
public function getWhy()
{
echo static::$why . "<BR>\n";
}
}
class B extends Base
{
protected static $why = "Want this value?";
}
$A = new A;
$A->callB();