You are here: Child class references static variable of parent instead of its own « PHP Programming Language « IT news, forums, messages
Child class references static variable of parent instead of its own

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация