title: PHP, traits - static variables and functions
body: <p>I have trait <code>ExportOptions</code> with two static variables and two functions.</p>
  
  <p>Variables are <code>$Code</code> and <code>$ExportType</code>. Content of <code>$ExportType</code> is set in function <code>Set_ExportType</code>. And both variables meet each other in function <code>Convert_ExportCode</code>.</p>
  
  <p>Function <code>Convert_ExportCode</code> has following simple code:</p>
  
  <pre><code>private function Convert_ExportCode()
  {
      switch(static::$ExportType)
      {
          /*
           * writes code
          */
          case UniT::UNIT_OPTION_END:
              self::$GlobalCode[] = $this -&gt; LocalCode;
              $Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
              self::$GlobalCode = array();
              echo $Text;
              break;
          /*
           * exports code without writing
          */
          case UniT::UNIT_OPTION_STEP:
              self::$GlobalCode[] = $this -&gt; LocalCode;
              $Text = preg_replace('/([\n]+)/', "\n", implode('', self::$GlobalCode));
              self::$GlobalCode = array();
              return $Text;
              break;
          /*
           * saves part of code
          */
          default:
              self::$GlobalCode[] = $this -&gt; LocalCode;
              for($Order = 0; $Order &lt; count(self::$GlobalCode); $Order++)
              {
                  self::$GlobalCode[$Order] = preg_replace('/([\n]+)/', "\n", self::$GlobalCode[$Order]);
              }
      }
  }
  </code></pre>
  
  <p>and it is (as whole trait, of course) used by three classes (<code>CodeGenerator</code>, <code>SimpleAssembler</code>, <code>MenuAssembler_SelectOptgroup</code>). It would be not problem, if those classes could not use each other.</p>
  
  <p>Class <code>CodeGenerator</code> is used by both other classes - and class <code>SimpleAssembler</code> is used by class <code>MenuAssembler_SelectOptgroup</code>.</p>
  
  <p>I know (because I tested it - and that class is on the first level) that <code>CodeGenerator</code> accepts and outputs (and handles) content of variable <code>self::$Code</code> as it should, even if it gets as text wrapped into any closed element code generated.</p>
  
  <pre><code>public function Execute()
  {
      $CodePartNumber = count(self::$Code);
  
      /* some code that not handles with sel::$Code */
  
      // base of code storage into self::$Code
      self::$Code[$CodePartNumber] = $this -&gt; Get_AssembledCode(/* arguments for vsprintf */);
  
      return $this -&gt; Convert_ExportCode();
  }
  </code></pre>
  
  <p>Class <code>SimpleAssembler</code> assembles and stores code in similar way:</p>
  
  <pre><code>public function Execute()
  {
      $this -&gt; Check_Orders();
      $CodePartNumber = count(self::$Code);               
  
      /*
       * generation of sub-level
       */
      for($Order = 0; $Order &lt; count($this -&gt; Content); $Order++)
      {
          $VMaX = new CodeGenerator($this -&gt; Elements['sub']['main']);
          $VMaX -&gt; Set_Text((empty($this -&gt; Content[$Order]) ? '' : $this -&gt; Content[$Order] ));
  
          if($Order &lt; count($this -&gt; Content)-1 )
          {
              $VMaX -&gt; Set_ExportType(UniT::UNIT_OPTION_GOON);
              $VMaX -&gt; Execute();
          }
          else
          {
              $VMaX -&gt; Set_ExportType(UniT::UNIT_OPTION_STEP);
              self::$Code[$CodePartNumber] = $VMaX -&gt; Execute();
          }
  
  
      }
  
      if($this -&gt; Disable_TopLevel == FALSE)
      {
          /*
           * generation of top level element and inserting of columns into it
          */
          $VMaX = new CodeGenerator($this -&gt; Elements['top']['main']);
          $VMaX -&gt; Set_ExportType(static::$ExportType);
          $VMaX -&gt; Set_Text(self::$Code[$CodePartNumber]);
          return $VMaX -&gt; Execute();
      }
      else
      {
          return $this -&gt; Convert_ExportCode();
      }
  }
  </code></pre>
  
  <p>But class 'SimpleAssembler' has a problem with accepting of content of this variable. And in final output is</p>
  
  <pre><code>&lt;ol &gt;
      &lt;ol &gt;   
          &lt;li &gt;1&lt;/li&gt;
          &lt;li &gt;2&lt;/li&gt;
          &lt;li &gt;3&lt;/li&gt;     
      &lt;/ol&gt;
      &lt;li &gt;4&lt;/li&gt;
      &lt;li &gt;5&lt;/li&gt;
      &lt;li &gt;6&lt;/li&gt;
  &lt;/ol&gt;
  </code></pre>
  
  <p>instead</p>
  
  <pre><code>&lt;ol &gt;
      &lt;li &gt;1&lt;/li&gt;
      &lt;li &gt;2&lt;/li&gt;
      &lt;li &gt;3&lt;/li&gt;     
  &lt;/ol&gt;
  
  &lt;ol &gt;
      &lt;li &gt;4&lt;/li&gt;
      &lt;li &gt;5&lt;/li&gt;
      &lt;li &gt;6&lt;/li&gt; 
  &lt;/ol&gt;
  </code></pre>
  
  <p>I would like to know how to solve it.</p>
  
  <p><strong>Edit:</strong>
  I tried to use also non-static variable for in-class code storage between various operations - and give only final code to that static variable, and even if I added</p>
  
  <pre><code>$this -&gt; LocalCode = NULL;
  </code></pre>
  
  <p>to constructor and destructor, result is still the same.</p>
  
  <p><strong>Edit:</strong>
  I improved code in the beginning of question to new version, as is currently.</p>