title: Php empty file output in flat file ORM class
body: <p><strong>Background :</strong> </p>
  
  <p>I am trying to create a flat file key value store. When i use the code below the array that has been written to the file gets deleted and i end up with an empty file. On the next request the file gets populated with the array.  This process loops itself on every page request. </p>
  
  <p><strong>What i have tried :</strong> </p>
  
  <ul>
  <li>Various methods with "json_encode()"/"json_decode()".</li>
  <li>Also tried to do the same with "serialize()"/"unserialize()". </li>
  <li>Tried various coding structures with "array_merge()" and "array_merge_recursive()".</li>
  </ul>
  
  <p>Nothing seems to work! I end up with the same loop of an empty file -> file with array and it continues </p>
  
  <p><strong>Question :</strong> </p>
  
  <p>Can someone more experienced tell me what i am doing wrong?</p>
  
  <p><strong>code :</strong> </p>
  
  <pre><code>/**
   * Class Orm
   */
  class Orm
  {
  
      /**
       * @var string The Root database directory with a trailing slash. default is "./database/".
       */
      static $dir = "./database/";
      /**
       * @var array
       */
      protected $data;
      /**
       * @var string
       */
      protected $file = "";
  
      /**
       * @param $file
       * @return string
       */
      public function load_table($file)
      {
          try {
              if (file_exists(self::$dir . $file)) {
                  return $this-&gt;file = $file;
              } else {
                  throw new Exception("The file " . $file . " cannot be created, because it already exists");
              }
          } catch (Exception $error) {
              return $error-&gt;getMessage();
          }
      }
  
      /**
       * @param String
       * @param array $values An associative array of values to store.
       * @return array
       */
      public function set($key, $values = array())
      {
          try{
              if (!empty($key) &amp;&amp; !empty($values)){
                  return $this-&gt;data[$key] = $values;
              } else {
                  throw new Exception();
              }
          } catch (Exception $error){
              return $error-&gt;getMessage();
          }
  
      }
  
      public function save()
      {
          try{
              if (file_exists(self::$dir . $this-&gt;file)) {
                  if (filesize(self::$dir . $this-&gt;file) == 0)
                  {
                      file_put_contents(self::$dir . $this-&gt;file, print_r($this-&gt;data, TRUE));
                  }else{
                      $tmp = file_get_contents(self::$dir . $this-&gt;file);
                      $content = array_merge($tmp, $this-&gt;data);
                      file_put_contents(self::$dir . $this-&gt;file, print_r($content, TRUE));
                  }
              } else {
                  throw new Exception();
              }
          } catch(Exception $error){
              return $error-&gt;getMessage();
          }
  
      }
  }
  
  
  $user = new Orm();
  $user-&gt;load_table("users");
  $user-&gt;set("Tito",array("age" =&gt; "32", "occupation" =&gt; "cont"));
  $user-&gt;save();
  </code></pre>
  
  <p>PS : I thought this would be a nice project to familiarize myself with Php. So please do not advise to use SQL as this is for learning and understanding Php only.</p>