note 50645 deleted from language.oop5.paamayim-nekudotayim by sobak
| From: | sobak@php.net | Date: | Thu, 01 Dec 2016 05:55:50 +0000 |
| Subject: | note 50645 deleted from language.oop5.paamayim-nekudotayim by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207785@lists.php.net to get a copy of this message | ||
Note Submitter: thenewparadigm at hotmail dot com
----
There is also a quirk with using the scope resolution operator on static class variables. Below is
an example using a highly modified version of Ian's code:
<?php
class ExampleSuperclass
{
static $className;
static function showClassName() {
echo self::$className . "\n";
}
}
class ExampleSubclassOne extends ExampleSuperclass
{
static function setClassName()
{
self::$className = "subclassOne";
}
}
class ExampleSubclassTwo extends ExampleSuperClass
{
static function setClassName()
{
self::$className = "subclassTwo";
}
}
// setting variables for each class
ExampleSubclassOne::setClassName();
ExampleSubclassTwo::setClassName();
ExampleSubclassOne::showClassName(); // output is "subclassTwo"!
// more output:
echo ExampleSubclassOne::$className . "\n"; // output is "subclassTwo"!
echo ExampleSubclassTwo::$className . "\n"; // output is "subclassTwo"
echo ExampleSuperclass::$className . "\n"; // output is "subclassTwo"!
?>
appearantly, any static variables defined in a superclass are directly referenced in subclasses,
and all changes are visible throughout the class heirarchy. care must be taken when using static
class variables.