title: static variables in php not holding value
body: <p>I have written a demo program to describe a problem I'm having with static variables in php.  I have a singleton class, History.php, that contains a static $idx variable intially set to -1.  An incIdx() function in the History class increments $idx. The demo starts by calling index.php which gets an instance of History, repeatedly calls incIdx() and displays the $idx final value. Then, on clicking a button, test.php is called which gets the History instance, repeatedly calls incIdx(), and displays the final value. What I don't understand is, the value created by index.php is not the starting value for the test.php. That is, index.php and test.php both start with the initial value = -1.
  The code follows:</p>
  <pre><code>&lt;?php
  class History {
      private static int $idx;
      private static $instance = null;
      
      private function __construct() {
          $this::$idx = -1;
      }
      
      public static function getHistory() {
          if (self::$instance === null) {
              self::$instance = new History();
          } 
          return self::$instance;
      }
      
      public function incIdx() {
          $this::$idx++;
      }
      
      public function getIdx(): int {
          return $this::$idx;
      }
  }
  
  &lt;?php
  require_once &quot;History.php&quot;;
  
  $instance = History::getHistory();
  for ($i = 0; $i &lt; 5; $i++) {
      $instance-&gt;incIdx();
  }
  $value = $instance-&gt;getIdx();
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;UTF-8&quot;&gt;
          &lt;title&gt;Test Static&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;In index.php&lt;/h1&gt;
          &lt;h2&gt;Current idx = &quot;&lt;?php echo $value; ?&gt;&quot;&lt;/h2&gt;
          &lt;button type=&quot;button&quot;&gt;&lt;a href=&quot;test.php&quot;&gt;Click to continue&lt;/a&gt;&lt;/button&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  
  &lt;?php
  require_once &quot;History.php&quot;;
  
  $instance = History::getHistory();
  for ($i = 0; $i &lt; 5; $i++) {
      $instance-&gt;incIdx();
  }
  $value = $instance-&gt;getIdx();
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta charset=&quot;UTF-8&quot;&gt;
          &lt;title&gt;Test Static&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;In test.php&lt;/h1&gt;
          &lt;h2&gt;Current idx = &quot;&lt;?php echo $value; ?&gt;&quot;&lt;/h2&gt;
      &lt;/body&gt;
  &lt;/html&gt;```
  </code></pre>