title: How can I re-assign a value to an object variable from the object itself in PHP?
body: <p>I'm creating a class with string validating methods. I cannot re-assign a value to the object reference like this:</p>
  
  <pre><code>class clearText
  {
      private $text;
  
      function __construct($input)
      {
          $this-&gt;text = $input;
      }
  
      public function clearUp()
      {
          // …
          // functions to sanitize the string in $this-&gt;text
          // …
          $this = $this-&gt;text;
      }
  }
  
  $content = new clearText($_POST['content']);
  $content-&gt;clearUp();
  </code></pre>
  
  <p>as the above example outputs:</p>
  
  <blockquote>
    <p><b>Fatal error</b>:  Cannot re-assign $this in <b>\clearText.php</b> on line <b>13</b></p>
  </blockquote>
  
  <p>When I call <code>clearUp()</code>, I don't need the object anymore, so I would like to avoid specifying this assignment like here, every time I call the method:</p>
  
  <pre><code>$content = new clearText($_POST['content']);
  $content-&gt;clearUp();
  $content = $content-&gt;text;
  </code></pre>
  
  <p>Is there any way to do this <em>inside the method</em>?</p>
  
  <hr>
  
  <h2>A possible answer</h2>
  
  <p>Somebody suggested returning the value, so I can re-assign it to the object variable in the same statement, that execute the method. The answer has since been deleted, but it works for what I need.</p>
  
  <p>Method definiton:</p>
  
  <pre><code>public function clearUp()
  {
      // …
      // functions to sanitize the string in $this-&gt;text
      // …
      return $this-&gt;text;
  }
  </code></pre>
  
  <p>When instantiated:</p>
  
  <pre><code>$content = new clearText($_POST['content']);
  $content = $content-&gt;clearUp();
  </code></pre>