note 53915 added to language.variables.scope
| From: | jameslee at cs dot nmt dot edu | Date: | Thu, 16 Jun 2005 21:33:36 +0000 |
| Subject: | note 53915 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90556@lists.php.net to get a copy of this message | ||
It should be noted that a static variable inside a method is static across all instances of that
class, i.e., all objects of that class share the same static variable. For example the code:
<?php
class test {
function z() {
static $n = 0;
$n++;
return $n;
}
}
$a =& new test();
$b =& new test();
print $a->z(); // prints 1, as it should
print $b->z(); // prints 2 because $a and $b have the same $n
?>
somewhat unexpectedly prints:
1
2
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53915
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53915&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53915&report=yes
Search -- http://master.php.net/manage/user-notes.php