title: When should I declare variables in a PHP class?
body: <p>I'm new to the OOP paradigm, so there's probably a simple explanation for this question...</p>
  
  <p>Do you always need to declare public object-wide variables in a class? For example:</p>
  
  <pre><code>&lt;?php
  
  class TestClass
  {
      var $declaredVar;
  
      function __construct()
      {
          $this-&gt;declaredVar = "I am a declared variable.";
          $this-&gt;undeclaredVar = "I wasn't declared, but I still work.";
      }
  
      function display()
      {
          echo $this-&gt;declaredVar . "&lt;br /&gt;";
          echo $this-&gt;undeclaredVar;
          echo "&lt;br /&gt;&lt;br /&gt;"; 
      }
  }
  
  $test = new TestClass;
  $test-&gt;display();
  
  $test-&gt;declaredVar = "The declared variable was changed.";
  $test-&gt;undeclaredVar = "The undeclared variable was changed.";
  
  $test-&gt;display();
  
  ?&gt;
  </code></pre>
  
  <p>In this code, even though <code>$declaredVar</code> is the only declared variable, <code>$undeclaredVar</code> is just as accessible and useable--it seems to act as if I had declared it as public. </p>
  
  <p>If undeclared class variables are always accessible like that, what's the point of declaring them all up front? </p>