Posted by Richard K Miller on 08/17/07 20:51
Here's an OOP question that perplexes me. It seems PHP doesn't treat
static variables correctly in child classes.
<?php
class ABC {
public $regular_variable = "Regular variable in ABC\n";
public static $static_variable = "Static variable in ABC\n";
public function regular_function() {
echo $this->regular_variable;
}
public static function static_function() {
echo self::$static_variable;
}
}
class DEF extends ABC {
public $regular_variable = "Regular variable in DEF\n";
public static $static_variable = "Static variable in DEF\n";
}
$abc = new ABC();
$abc->regular_function();
ABC::static_function();
$def = new DEF();
$def->regular_function();
DEF::static_function();
?>
WHAT I EXPECTED:
Regular variable in ABC
Static variable in ABC
Regular variable in DEF
Static variable in DEF
ACTUAL OUTPUT:
Regular variable in ABC
Static variable in ABC
Regular variable in DEF
Static variable in ABC <--- This is different from what I expected
Anyone know why?
There are several examples of this bug (if it is one) in the comments
for the get_class() function: http://us.php.net/manual/en/function.get-class.php
Best regards,
Richard
Navigation:
[Reply to this message]
|