Re: Static variable in a class method
| From: | Jochem Maas | Date: | Thu, 14 Feb 2008 11:37:37 +0000 |
| Subject: | Re: Static variable in a class method | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-269290@lists.php.net to get a copy of this message | ||
Nathan Nobbe schreef:
On Feb 13, 2008 8:44 PM, Nirmalya Lahiri <nirmalyalahiri@yahoo.com> wrote:actually it pretty much *is* the same - the static class member will exhibit the same behaviour, only the scope is different.--- Pauau <wakamonka747@hotmail.com> wrote:what you are using is potentially not what you think it is. you are using a 'static variable' which is not a static class member.I have a class method which declares a static variable within.However, across all the instances of the class, the current value on that variable replicates. Is it the intended functionality? Example: class A { publicPauau, Please visit the link below for help.. http://www.php.net/manual/en/language.oop5.static.phpfunction foo() { static $i=0; $i++; }}$obj1 = newA();$obj1->foo(); //$i = 1 $obj2 = new A();$obj2->foo(); //$i = 2 where I think it should be 1, becaue it's a new instance.
you can find the doc on static variables here, http://www.php.net/manual/en/language.variables.scope.php im not sure if their behavior is well defined when they are used in classes, or objects.behaviour is indentical to usage inside standalone functions.
as Nirmalya, has alluded, you should check out the docs on static class members. im sure that you can achieve whatever you need to by using some combination of static class members and instance variables. -nathan