note 51640 added to language.oop5.static
| From: | php-general at lists dot php dot net | Date: | Wed, 06 Apr 2005 10:14:39 +0000 |
| Subject: | note 51640 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-87567@lists.php.net to get a copy of this message | ||
You misunderstand the meaning of inheritance : there is no duplication of members when you inherit
from a base class. Members are shared through inheritance, and can be accessed by derived classes
according to visibility (public, protected, private).
The difference between static and non static members is only that a non static member is tied to an
instance of a class although a static member is tied to the class, and not to a particular instance.
That is, a static member is shared by all instances of a class although a non static member exists
for each instance of class.
Thus, in your example, the static property has the correct value, according to principles of object
oriented conception.
class Base
{
public $a;
public static $b;
}
class Derived extends Base
{
public function __construct()
{
$this->a = 0;
parent::$b = 0;
}
public function f()
{
$this->a++;
parent::$b++;
}
}
$i1 = new Derived;
$i2 = new Derived;
$i1->f();
echo $i1->a, ' ', Derived::$b, "\n";
$i2->f();
echo $i2->a, ' ', Derived::$b, "\n";
outputs
1 1
1 2
----
Manual Page -- http://www.php.net/manual/en/language.oop5.static.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+51640
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+51640&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+51640&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+51640&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+51640&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+51640&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+51640&report=yes
Search -- http://master.php.net/manage/user-notes.php