note 108650 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 14 May 2012 21:30:28 +0000 |
| Subject: | note 108650 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187977@lists.php.net to get a copy of this message | ||
Note Submitter: nick at nickvahahlik dot com
----
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'