title: fwrite() is not a valid stream resource
body: <p>I've created a class <code>Log</code> that write some content on file (<em>this allow me to debug my application in production</em>). Now this <code>Log</code> class is included into a <code>SingleTon</code> class called <code>Trace</code>, so for use the <code>Log</code> class method I simply do:</p>
  
  <pre><code> Trace::getInstance()-&gt;info("Write some content.");
  </code></pre>
  
  <p>this is the <code>Trace</code> class:</p>
  
  <pre><code>&lt;?php
  
  require_once("Log.php");
  
  class Trace
  {
      private $_trace;
      private static $_instance;
  
      private function __construct()
      {
         $this-&gt;_trace = new Log();
      }
  
      public static function getInstance()
      {
         if(!isset(self::$_instance))
         {
            self::$_instance = new static;
         }
         return self::$_instance;
      }
  
  }
  </code></pre>
  
  <p>in the <code>Log</code> class I have a <code>destruct</code> that destroys the read and write streams, like this:</p>
  
  <pre><code>if($this-&gt;_handle)
  {
       fclose($this-&gt;_handle);
  }
  </code></pre>
  
  <p><code>$_handle</code> contains this: <code>Resource id #22</code>.
  Now if I execute one call like this: <code>Trace::getInstance()-&gt;info("Write some content.");</code>, the system working well and the text is wrote correctly on the file, but, if I do this:</p>
  
  <pre><code>Trace::getInstance()-&gt;info("Write some content.");
  Trace::getInstance()-&gt;info("Write some content2.");
  </code></pre>
  
  <p>I get this error:</p>
  
  <blockquote>
    <p>fwrite(): 22 is not a valid stream resource</p>
  </blockquote>
  
  <p>I thought about an instance of class <code>Trace</code> that remained open so I tried with:</p>
  
  <pre><code>function __destruct()
  {
      unset(self::$_instance);
  }
  </code></pre>
  
  <p>for destroy the <code>Log</code> instance for each call. But I got this error:</p>
  
  <blockquote>
    <p>Attempt to unset static property Trace::$_instance in ... </p>
  </blockquote>
  
  <p>I don't know what is wrong, someone could help me to figure out?</p>
  
  <p><strong>UPDATE - Log Class</strong></p>
  
  <p>For write on the <code>Log</code> class, I do this:</p>
  
  <pre><code>if(fwrite($this-&gt;_handle, $mex) === false)
  </code></pre>
  
  <p>where <code>$this-&gt;_handle</code> represents the resource like <code>stream</code> and <code>$mex</code> is the message to write. Now I fall in the Exception:</p>
  
  <pre><code>if(fwrite($this-&gt;_handle, $mex) === false)
  {
      throw new RuntimeException("Can't write.");
  }
  </code></pre>
  
  <p>only on the second attempts, for the first attempt I can write correctly on the file. In the destruct of log file I've:</p>
  
  <pre><code>public function __destruct()
  {
      // Close the stream
  
      if($this-&gt;_handle)
      {
          fclose($this-&gt;_handle);
      }
  }
  </code></pre>
  
  <p>If I use the <code>Log</code> class without static instance I've no problem, for example like this:</p>
  
  <pre><code>&lt;?php
  
  include "log.php";
  
        $log = new Log();
  
        $log-&gt;info("test");
        $log-&gt;error("test2");
  ...
  </code></pre>
  
  <p>no error happean. </p>