title: In PHP classes is it possible to create a private variable inside of a function?
body: <p>In using PHP classes I have noticed that inside a class when I define a variable in a function as a property of that class in the <code>$this-&gt;variablename</code> way it automatically becomes a public variable of that class.</p>
  
  <pre><code>class example {
      public function setstring() {
          $this-&gt;string = "string";
      }
  }
  </code></pre>
  
  <p>So that </p>
  
  <pre><code>$class = new example();
  echo $class-&gt;string; 
  </code></pre>
  
  <p>Outputs: <code>string</code></p>
  
  <p>However, in the case that I wanted to create private variables only accessible to functions inside the class, is there anyway to declare them only inside of the <code>setstring()</code> function? Instead of declaring them as private outside of the function like this.</p>
  
  <pre><code>class example {
      private $string ='';
      public function setstring() {
          $this-&gt;string = "string";
      }
  }
  </code></pre>
  
  <p>The reasons someone might do this are for neatness, so as not to have a long list of private variables declared at the beggining of a class.</p>