title: is it acceptable to recycle or reuse variables?
body: <p>I've looked everywhere, and I can't seem to find an answer one way or another. Is it acceptable (good or bad practice) to reuse or recycle a variable? It works, and I've used this method a few times, but I don't know if I should be doing this or not. I'm trying to get away from using static methods, and moving into dependency injection.</p>
  
  <p>in this example, the $table_name is set elsewhere. </p>
  
  <pre><code>class DbObject {
      private $db = NULL;
      protected $table_name;
  
      public function __construct($dbh, $item) {
          $this-&gt;db = $dbh;
          $this-&gt;$table_name = $item;
      }
  
      // counts items in database //
      public function count_all() {
          try {
              $sql = 'SELECT COUNT(*) FROM ' . $this-&gt;table_name;
  
              $stmt = $this-&gt;db-&gt;query($sql);
              $stmt-&gt;setFetchMode(pdo::FETCH_COLUMN, 0);
              $result = $stmt-&gt;fetchColumn();
              return $result;
          } catch (PDOException $e) {
              echo $e-&gt;getMessage());
          }
  }
  </code></pre>
  
  <p>To use this I would use it like this:</p>
  
  <pre><code>$total_count = new DbObject(new Database(), 'items');
  $total_count = $total_count-&gt;count_all();
  </code></pre>
  
  <p>Is this an acceptable way to code? 
  Thanks for your help. </p>