0

I come from OO background and have done a 3 day crash course on PHP and now I try to find few issues in a relatively big PHP project and the problem I am getting seems to be very weird (at least in other OO languages)

So what I have is something like this:

class A
{
    static $num = 0;

    public static function instance($type, $options = array())
    {
        self::$num++;
        // some more code here
    }
}

and after few dozens of calls to this method $num value resets again to 0 and I wonder how is this possible? I know that in other languages like C# the lifetime of the static is the lifetime of the app. So what are potential reasons for static variable reset in PHP

Vitalij
  • 4,587
  • 9
  • 42
  • 65
  • 1
    It's not possible. If it changes its value - then something changed it. Change it to `private` first and see the difference. PS: using statics isn't "true" `OO` but just working with classes – zerkms Feb 11 '14 at 21:15
  • possible duplicate of [How do I change a static variables value in PHP?](http://stackoverflow.com/questions/9497161/how-do-i-change-a-static-variables-value-in-php) – user1767754 Feb 11 '14 at 21:15
  • http://stackoverflow.com/questions/9497161/how-do-i-change-a-static-variables-value-in-php – user1767754 Feb 11 '14 at 21:15
  • 2
    Your comment about the "lifetime of the app" is probably the key to the mystery. What environment are you running PHP in? If it's a webserver then there may be multiple instances of the app and they may get restarted at various (arbitrary to you) times. One common way to configure PHP in a webserver is to have scripts restart after a certain number of accesses which might explain the behavior you're seeing. – Perry Feb 11 '14 at 21:16
  • If he changes it to private he won't be able to access it from a static function. – Peter Smartt Feb 11 '14 at 21:17
  • 2
    @Peter Smartt: uhm, what? What if you try it first? – zerkms Feb 11 '14 at 21:17
  • 1
    You need to share more information. Normally with PHP (in most cases) your program runs again for *every request*, so the lifetime is pretty short - might that be the problem? – kapa Feb 11 '14 at 21:17
  • It would only happen if, somewhere in your code, the static property is set to zero. Change the visibility of the variable to either `protected` or `private` to ensure that it cannot be changed from outside the class. You should also search through your code for something like `::$num` to see if it is changed somewhere, just to be sure. – Sverri M. Olsen Feb 11 '14 at 21:21
  • The only way the var could get reset to zero is if some code elsewhere set it to zero, or something completely reloaded the state of the PHP interpreter so that the object was re-initialized to zero. – Marc B Feb 11 '14 at 21:22
  • @zerkms: "Fatal error: Access to undeclared static property" - but I just realised that you probably meant "Change it to 'private static' first ..." – Peter Smartt Feb 11 '14 at 21:45

1 Answers1

0

If this is being run from HTTP requests, when a user changes page or the connection is closed and re-opened, then a completely new instance if your program will result. If this is the case, you may need to implement session variables.

Another way you could be coming across this issue is by referencing the class and instantiations of the class (objects) separately and obtaining different values. I'm not too sure how PHP specifically handles this behaviour with statics, but writing a test similar to this question may be useful.

Community
  • 1
  • 1
lberezy
  • 450
  • 5
  • 19