title: Indirect modification of overloaded property
body: <p>I'm creating a forum, and I want to keep track of which threads have been updated since the user last visited. So I have an array that I keep in $_SESSION that is basically structured as <code>[$boardid][$threadid] = 1</code>. If the threadid and boardid are set, then the thread has not been read and the board contains unread threads. When a user views a thread, I just <code>unset()</code> the appropriate board and thread id. However, I've having problems with getting unset to work with arrays like this.</p>
  
  <p>Firstly, I have a session class to make handling session data a little nicer</p>
  
  <pre><code>class Session {
  private $_namespace;
  
  public function __construct($namespace = '_default') {
      $this-&gt;_namespace = $namespace;
  }
  
  /**
   * Erase all variables in the namespace
   */
  public function clear() {
      unset($_SESSION[$this-&gt;_namespace]);
  }
  
  public function __set($name, $value) {
      $_SESSION[$this-&gt;_namespace][$name] = $value;
  }
  
  public function __get($name) {
      if(isset($_SESSION[$this-&gt;_namespace]) &amp;&amp; array_key_exists($name, $_SESSION[$this-&gt;_namespace])) {
          return $_SESSION[$this-&gt;_namespace][$name];
      }
  
      return null;
  }
  
  public function __isset($name) {
      return isset($_SESSION[$this-&gt;_namespace][$name]);
  }
  
  
  public function __unset($name) {
      unset($_SESSION[$this-&gt;_namespace][$name]);
  }
  
  };
  </code></pre>
  
  <p>Then I have a CurrentUser class representing the current user. The CurrentUser class has a member named _data which is-a Session object. In the CurrentUser class I override the __get and __set methods to use the _data member.</p>
  
  <pre><code>    public function __set($name, $value) {
      $this-&gt;_data-&gt;$name = $value;
  }
  
  public function __isset($name) {
      return isset($this-&gt;_data-&gt;$name);
  }
  
  public function __get($name) {
      if(isset($this-&gt;_data-&gt;$name)) {
          return $this-&gt;_data-&gt;$name;
      }
  
      return null;
  }
  </code></pre>
  
  <p>Now to keep track of which threads have been unread, I fetch all threads whose date is >= the user's last_seen date. I also have methods to remove board and threads from the array.</p>
  
  <pre><code>    public function buildUnreadList($since) {
      // Build a "new since last visit" list
      $forumModel = new Model_Forum();
      $newThreads = $forumModel-&gt;fetchThreadsSinceDate($since);
      foreach($newThreads as $thread) {
          $tmp =&amp; $this-&gt;unreadThreadsList;
          $tmp[$thread['board']][$thread['id']] = 1;              
      }
  }
  public function removeThreadFromUnreadList($boardid, $threadid) {
      $threads =&amp; $this-&gt;unreadThreadsList;
      unset($threads[$boardid][$threadid]);   
  }
  
  public function removeBoardFromUnreadList($boardid) {
      $threads =&amp; $this-&gt;_data-&gt;unreadThreadsList;
      unset($threads[$boardid]);
  }
  </code></pre>
  
  <p>This is where I'm running into problems. I'm getting a <code>Indirect modification of overloaded property Session::$unreadThreadsList has no effect</code> error on <code>$threads =&amp; $this-&gt;_data-&gt;unreadThreadsList;</code> How can I either fix this problem or design a better solution? I thought about creating a class that keeps track of the array so I don't have to have an array of arrays of arrays of arrays, but I'm not certain on persisting objects and creating an object just to manage an array feels really dirty to me.</p>