title: accessing private variable from member function in PHP
body: <p>I have derived a class from <code>Exception</code>, basically like so:</p>
  
  <pre><code>class MyException extends Exception {
  
      private $_type;
  
      public function type() {
          return $this-&gt;_type; //line 74
      }
  
      public function __toString() {
  
          include "sometemplate.php";
          return "";
  
      }
  
  }
  </code></pre>
  
  <p>Then, I derived from <code>MyException</code> like so:</p>
  
  <pre><code>class SpecialException extends MyException {
  
      private $_type = "superspecial";
  
  }
  </code></pre>
  
  <p>If I <code>throw new SpecialException("bla")</code> from a function, catch it, and go <code>echo $e</code>, then the <code>__toString</code> function should load a template, display that, and then not actually return anything to echo.</p>
  
  <p>This is basically what's in the template file</p>
  
  <pre><code>&lt;div class="&lt;?php echo $this-&gt;type(); ?&gt;class"&gt;
  
      &lt;p&gt; &lt;?php echo $this-&gt;message; ?&gt; &lt;/p&gt;
  
  &lt;/div&gt;
  </code></pre>
  
  <p>in my mind, this should definitely work. However, I get the following error when an exception is thrown and I try to display it:</p>
  
  <blockquote>
    <p><strong>Fatal error</strong>: Cannot access private property SpecialException::$_type in <strong>C:\path\to\exceptions.php</strong> on line <strong>74</strong></p>
  </blockquote>
  
  <p>Can anyone explain why I am breaking the rules here? Am I doing something horribly witty with this code? Is there a much more idiomatic way to handle this situation? The point of the <code>$_type</code> variable is (as shown) that I want a different div class to be used depending on the type of exception caught.</p>