0

I've noticed that the keyword static in PHP is not that static at all.

Lets say Elmo is my singleton:

class Elmo
{
    private static $instance;

    private function __construct()
    {
        echo 'Elmo says constructor\n';
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
            self::$instance = new Elmo();

        return self::$instance;
    }

    public function boo()
    {
        echo 'Elmo says boo!\n';
    }
}

And the following file is just a regular .php script.

<?php

    Elmo::getInstance()->boo();
    Elmo::getInstance()->boo();

    // Output:
    // Elmo says constructor
    // Elmo says boo!
    // Elmo says boo!

?>

Every new page Elmo gets re-constructed. Why don't subsequent pages have the following output?

<?php

    // Output:
    // Elmo says boo!
    // Elmo says boo!

?>

I hope someone can enlighten me on this, thanks!

Kevin
  • 5,626
  • 3
  • 28
  • 41
  • 1
    That's one of the reasons why you dont want to use Singletons in PHP. It's pointless when there is no shared application memory. See http://stackoverflow.com/questions/4595964/who-needs-singletons/4596323#4596323 – Gordon Feb 12 '11 at 09:12
  • @Gordon - That is some very useful information, thank you. – Kevin Feb 12 '11 at 10:52

3 Answers3

9

because on every page load all memory is wiped ?

Rasmus Styrk
  • 1,296
  • 2
  • 20
  • 36
  • So the only way to reach the `static` I need is to shove it into a session..? – Kevin Feb 12 '11 at 08:45
  • 3
    That's what sessions are for - data persistence between page requests – Sam Dufel Feb 12 '11 at 08:47
  • Iam not really sure you can save an object in a session (try it out). static and other methods only lives while its being rendered, this is why scripting languages like php wont eat your server up and that you dont need to know anything about momory management to code it. – Rasmus Styrk Feb 12 '11 at 08:52
  • Yes you can store objects in session. You just have to make sure that their type (class) is defined when loading them from session again, or you otherwise end up with a __PHP_Imcomplete_Class. – netcoder Feb 12 '11 at 08:55
8

Static scoping does not mean it will stay in memory forever, it means that the variable operates outside the program call stack, and will persist during the execution of the script. It is still cleared after the program ends.

netcoder
  • 66,435
  • 19
  • 125
  • 142
3

This is because every time you do a page load it runs {main} separately. This would be like running a java program two separate times and the static property not being retained. Elmo::$instance will only remain instantiated in the context of the same script. If you want it to work across page loads, you can serialize it in the session (or DB) and check this instead of $instance each time:

const SESSION = 'session';
public static function inst() {
   !isset($_SESSION[self::SESSION]) and self::init();
   self::$inst = $_SESSION[self::SESSION];
   return self::$inst;
}
private static function init() {
   $_SESSION[self::SESSION] = new self;
}
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405