note 108650 added to language.variables.scope
| From: | nick at nickvahahlik dot com | Date: | Mon, 14 May 2012 21:29:54 +0000 |
| Subject: | note 108650 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187976@lists.php.net to get a copy of this message | ||
As sideshowAnthony at googlemail dot com mentioned, function defined static variables are persistent
across instances. They are only available inside the scope of the function and not within other
members of the class.
If you have recursive functions that build iteratively inside of a class instance, you might want to
consider using class variables (e.g. $this->_localVar) instead.
<?php
class A {
function foo($a = '') {
static $b;
if ($a != '') {
$b = $a;
}
echo $b;
}
}
$a = new A();
$a->foo('c'); // 'c'
$b = new A();
$b->foo(); // also 'c'
----
Server IP: 69.147.83.197
Probable Submitter: 71.41.56.66
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/108650
Del: integrated -- https://master.php.net/note/delete/108650/integrated
Del: useless -- https://master.php.net/note/delete/108650/useless
Del: bad code -- https://master.php.net/note/delete/108650/bad+code
Del: spam -- https://master.php.net/note/delete/108650/spam
Del: non-english -- https://master.php.net/note/delete/108650/non-english
Del: in docs -- https://master.php.net/note/delete/108650/in+docs
Del: other reasons-- https://master.php.net/note/delete/108650
Reject -- https://master.php.net/note/reject/108650
Search -- https://master.php.net/manage/user-notes.php