title: PHP class unset/purge private array variable
body: <p>I'm trying to make an error logging class, I have some functions to set up various output methods like DB, file, return and screen. I want all errors to be stored into an array and when <code>__destruct()</code> is called, I want to stop the client from waiting for data and the log details about errors the user experienced. This way they don't have to report errors to me.</p>
  
  <p>I have 2 modes, a simple GUI to test the functionality and the actual script generates responses in JSON, machine to machine. For GUI the final dump is fine but for JSON it destroys the response. So all error reporting is off and I have to handle any errors that would be dumped on screen myself, hence the <code>$return</code> in <code>function flush_log($return)</code> which make the function return a string if set to true.</p>
  
  <p>After reporting flushing the errors I want to:<code>unset($this-&gt;log_arrays)</code><br>
  or empty: <code>$this-&gt;log_arrays=Array();</code>, but it is out of scope - I understand why, my function uses a local copy - but how do I reset the array?</p>
  
  <p><strong>[EDIT]:</strong> </p>
  
  <p>I tried:</p>
  
  <ul>
  <li><code>$this-&gt;log_arrays = Array();</code></li>
  <li><code>$this-&gt;log_arrays = null;</code></li>
  <li><p>array popping:</p>
  
  <pre><code>for ($i = 1; count($this-&gt;log_arrays); $i++)  
  {  
      array_pop($this-&gt;log_arrays);  
  }  
  </code></pre></li>
  </ul>
  
  <p>But I think none of it <em>can</em> work because within a class function you work with copies of variables, so they're basically out of scope. </p>
  
  <p><strong>[/EDIT]:</strong></p>
  
  <p>This is an already simplified class.. : </p>
  
  <pre><code>&lt;?php
      class log_strings extends mysqli 
      {
          private $log_arrays = Array();
  
          public function __construct($output_to_file=false, $output_to_db=true, $fall_back_file=true, $arguments, $ip=null)
          {
              // Setup mysqli connection, file handle or report error if one or all have failed.
              // Also check wich outputs should be used and keep that info for later.
          }
  
          public function log($level, $string)
          {
              $log_arrays[] = Array('level' =&gt; $level, 'string' =&gt; $string);
          }
  
          public function __destruct()
          {
              $this-&gt;flush_log();
          }
  
          public function flush_log($return=false)
          {
              if (!isset($log_arrays) &amp;&amp; count($log_arrays) == 0)
              {
                  return true;
              }
  
              if ($return)
              {
                  return $this-&gt;return_output();
              }
              else
              {
                  $success = false;
  
                  // if enabled, output to db
                  if ($this-&gt;output_to_db) 
                  {
                      $success = $success || $this-&gt;mysqli_output();
                  }
  
                  // if enabled or if db failed and fallback is enabled, output to file
                  if ($this-&gt;output_to_file || ($this-&gt;fall_back_file &amp;&amp; !$success)) 
                  {
                      $success = $success || $this-&gt;file_output();
                  }
                  // if neither file or db succeeded, dump on screen
                  if ($success = false)
                  {
                      $this-&gt;screen_dump();
                  }
  
                  return true;
              }
              unset($this-&gt;log_arrays); // &lt;= This is what it is all about!
          }
  
          private function screen_dump()
          {
              foreach($this-&gt;log_arrays as $array)
              {
                  echo "&lt;strong&gt;{$array['level']}&lt;/strong&gt;{$array['string']}&lt;br/&gt;\n";
              }
          }
  
          private function mysqli_output()
          {
              // Output to db functionally equal to $this-&gt;screen_dump()
          }
  
          private function file_output()
          {
              // Output to file functionally equal to $this-&gt;screen_dump()
          }
  
          private function return_output()
          {
              // Return output functionally equal to $this-&gt;screen_dump()
          }
      }
  ?&gt;
  </code></pre>