title: PHP can't call class method more than once
body: <p>As a beginner in this PHP OOP classes world I need some help to understand what I've been doing wrong and what I can do better.</p>
  
  <p>I have the following code for my database connection:</p>
  
  <pre><code>class ConexaoPDO_PTA
  {
      private $host_pdo_pta    = DB_HOST_PTA;
      private $dbname_pdo_pta  = DB_NAME_PTA;
      private $user_pdo_pta    = DB_USER_PTA;
      private $pass_pdo_pta    = DB_PASS_PTA;
      private $dbh;
      private $error;
  
      public function __construct(){
          $dsn = 'odbc:Driver={SQL Server}; Server='.$this-&gt;host_pdo_pta.'; Database='.$this-&gt;dbname_pdo_pta.';';
  
          $options = array(
              PDO::ATTR_PERSISTENT    =&gt; true,
              PDO::ATTR_ERRMODE       =&gt; PDO::ERRMODE_EXCEPTION
          );
  
          try
          {
              $this-&gt;dbh = new PDO($dsn, $this-&gt;user_pdo_pta, $this-&gt;pass_pdo_pta, $options);
          }
          catch(PDOException $e)
          {
              $this-&gt;error = $e-&gt;getMessage();
          }
      }
  
      public function query($query){
          $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
      }
  
      public function bind($param, $value, $type = null){
          if (is_null($type)) {
              switch (true) {
                  case is_int($value):
                      $type = PDO::PARAM_INT;
                      break;
                  case is_bool($value):
                      $type = PDO::PARAM_BOOL;
                      break;
                  case is_null($value):
                      $type = PDO::PARAM_NULL;
                      break;
                  default:
                      $type = PDO::PARAM_STR;
              }
          }
          $this-&gt;stmt-&gt;bindValue($param, $value, $type);
      }
  
      public function execute(){
          return $this-&gt;stmt-&gt;execute();
      }
  
      public function fetchAllAssoc(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      }
  
      public function fetchAssoc(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetch(PDO::FETCH_ASSOC);
      }
  
      public function fetchAllObj(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_OBJ);
      }
  
      public function fetchObj(){
          $this-&gt;execute();
          return $this-&gt;stmt-&gt;fetch(PDO::FETCH_OBJ);
      }
  
      public function rowCount(){
          return count($this-&gt;stmt-&gt;fetchAll());
      }
  
      public function debugDumpParams(){
          return $this-&gt;stmt-&gt;debugDumpParams();
      }
  
      public function closeConnection(){
          return $this-&gt;dbh = null;
      }
  }
  </code></pre>
  
  <p>That works just fine.
  Then I was trying to make this to generate a graphic data:</p>
  
  <pre><code>class DadosGraph extends ConexaoPDO_PTA
  {
  
      public $_data;
      public $_diaMes;
      public $_mesAno;
      public $_exibirData;
  
      public $_dataInicial;
      public $_dataFinal;
  
      public $_tipoEmpresa;
      public $_visao;
      public $_linha1;
      public $_linha2;
      public $_linha3;
      public $_comporLinhas;
  
  
      public function visao($visao)
      {
          switch($visao)
          {
              case 'vs1':
                  $this-&gt;_visao = "VS1";
              break;
              case 'vs2':
                  $this-&gt;_visao = "VS2";
              break;
              case 'vs3':
                  $this-&gt;_visao = "VS3";
              break;
              case 'vs4':
                  $this-&gt;_visao = "VS4";
              break;
  
              default:
                  $this-&gt;_visao = null;
              break;
          }
      }
  
  
      public function defineData($varData)
      {
          list($mes, $dia, $ano) = explode("/", $varData);
          $this-&gt;_exibirData  = $dia."/".$mes;
          $this-&gt;_data        = $mes."/".$dia."/".$ano;
          $this-&gt;_diaMes      = $dia."/".$mes;
          $this-&gt;_mesAno      = $mes."/".$ano;
      }
  
      public function defineTipoEmpresa($TipoEmpresa = null, $Empresa = null)
      {
          if(($TipoEmpresa != null) AND ($Empresa != null)):
              $this-&gt;_tipoEmpresa = ", @".$TipoEmpresa." = '".$Empresa."'";
          else:
              $this-&gt;_tipoEmpresa = null;
          endif;
      }
  
  
      public function gerarGrafico()
      {
          $arrTotal = array_replace_recursive($this-&gt;_linha1, $this-&gt;_linha2, $this-&gt;_linha3);
  
          $result = array();
          foreach ($arrTotal as $key =&gt; $value) {
              $horario = $value['Hora'].":59";
  
              if( $value['Hora'] &gt;= date("H", time()) ):
                  $TarifaTotalHoje = null;
              else:
                  $TarifaTotalHoje = ', "data3":"'.$value['TarifaTotal_linha3'].'"';
              endif;
  
              $TarifaTotalSP = ', "data1":"'.$value['TarifaTotal_linha1'].'"';
              $TarifaTotalOntem = ', "data2":"'.$value['TarifaTotal_linha2'].'"';
  
              echo '{ "category": "'.$horario.'"'.$TarifaTotalSP.''.$TarifaTotalOntem.''.$TarifaTotalHoje.' },&lt;br&gt;';
          }
      }
  
  
      public function linha($linha, $data, $horaInicial, $horaFinal)
      {
  
          $this-&gt;_dataInicial = $data." ".$horaInicial;
          $this-&gt;_dataFinal = $data." ".$horaFinal;
  
          $this-&gt;query("execute spBilhetesEmitidosporEmpresa @Acao = '".$this-&gt;_visao."', @DataHoraVenda = '".$this-&gt;_dataInicial."', @DataHoraVendaFinal = '".$this-&gt;_dataFinal."' ".$this-&gt;_tipoEmpresa."");
          $this-&gt;execute();
          $SelectDiaSPReceita = $this-&gt;fetchAllAssoc();
  
          $ord = array();
          foreach ($SelectDiaSPReceita as $value){
              $ord[] = strtotime( $value['DataHoraVenda'] );
          }
  
          array_multisort($ord, SORT_ASC, $SelectDiaSPReceita);
  
          $i = 0;
          $arrChavesHorarios = array();
          while ($i &lt; 2400) {
              $a = sprintf("%04d", $i);
              $hora = substr( $a, 0, 2);
  
              $horario = $hora.":00";
              $arrChavesHorarios[] = $horario;
  
              $i = $i + 100;
          }
  
          $i = 0;
          $arrN = array();
          while ($i &lt; 24) {
              $arrN[] = $i;
              $i++;
          }
  
          $arrCombine = array_combine($arrChavesHorarios, $arrN);
  
          $arrayDiaSP = array();
          foreach ($SelectDiaSPReceita as $value) {
              $hora = substr( $value['DataHoraVenda'] , 11, -10);
              $dia = substr( $value['DataHoraVenda'] , 8, -13);
              $mes = substr( $value['DataHoraVenda'] , 5, -16);
              $ano = substr( $value['DataHoraVenda'] , 0, -19);
  
              if(isset($arrayDiaSP[$hora.":00"])):
                  $arrayDiaSP[$hora.":00"]['BilhetesVendidos']++;
                  $arrayDiaSP[$hora.":00"]['TarifaTotal'] += $value['TotalEmissao'];
              else:
                  $arrayDiaSP[$hora.":00"] = array( "Dia" =&gt; $dia, "Mes" =&gt; $mes, "Ano" =&gt; $ano, "Hora" =&gt; $hora, "BilhetesVendidos" =&gt; 1, "TarifaTotal" =&gt; number_format($value['TotalEmissao'], 2) );
              endif;
          }
  
  
          $arrZeradosSP = array();
          foreach ($arrChavesHorarios as $value) {
              if( !array_key_exists($value, $arrayDiaSP) ):
                  list($hora, $min) = explode(":", $value);
                  $arrZeradosSP[$value] = array( "Dia" =&gt; 0, "Mes" =&gt; 0, "Ano" =&gt; 0, "Hora" =&gt; $hora, "BilhetesVendidos" =&gt; 0, "TarifaTotal" =&gt; number_format(0, 2) );
              endif;
          }
  
          $arrayMerge1 = array_merge_recursive($arrayDiaSP, $arrZeradosSP);
          $arrayDiaSPNovo = array_combine(array_merge($arrayMerge1, $arrCombine), $arrayMerge1);
          ksort($arrayDiaSPNovo);
  
          $bilhetesVendidos = 0;
          $tarifaTotal      = 0;
  
          $arrTotalSP = array();
          foreach ($arrayDiaSPNovo as &amp;$entry) {
              $bilhetesVendidos += $entry['BilhetesVendidos'];
              $tarifaTotal      += $entry['TarifaTotal'];
  
              $entry['BilhetesVendidos'] = $bilhetesVendidos;
              $entry['TarifaTotal']      = $tarifaTotal;
  
              $arrTotalSP[] = array("Dia" =&gt; $entry["Dia"], "Mes" =&gt; $entry["Mes"], "Ano" =&gt; $entry["Ano"], "Hora" =&gt; $entry["Hora"], "BilhetesVendidos".$linha =&gt; $entry["BilhetesVendidos"], "TarifaTotal".$linha =&gt; number_format( $entry["TarifaTotal"] , 2, ".", "") );
          }
  
          $this-&gt;$linha = $arrTotalSP;
      }
  
  }
  </code></pre>
  
  <p>When I run something like:</p>
  
  <p>
  
  <pre><code>error_reporting(E_ALL);
  
  require_once("../classes/class.autoload.inc.php");
  
  echo "&lt;pre&gt;Up and running!&lt;/pre&gt;&lt;br&gt;&lt;br&gt;";
  
  $test = new DadosGraph;
  $test -&gt; visao("vs3");
  $test -&gt; defineTipoEmpresa();
  
  $test -&gt; defineData("10/05/2015");
  $test -&gt; linha("_linha1", $test-&gt;_data, "00:00:00", "23:59:59");
  
  $test -&gt; defineData("10/16/2015");
  $test -&gt; linha("_linha2", $test-&gt;_data, "00:00:00", "23:59:59");
  
  $test -&gt; defineData( date("m/d/Y") );
  $test -&gt; linha("_linha3", $test-&gt;_data, "00:00:00", "23:59:59");
  
  $test -&gt; gerarGrafico();
  
  echo "&lt;br&gt;&lt;br&gt;";
  
  $obj = new DadosGraph;
  $obj -&gt; visao("vs3");
  $obj -&gt; defineTipoEmpresa("Empresa", "nome_emp");
  
  $obj -&gt; defineData("10/05/2015");
  $obj -&gt; linha("_linha1", $obj-&gt;_data, "00:00:00", "23:59:59");
  
  $obj -&gt; defineData("10/16/2015");
  $obj -&gt; linha("_linha2", $obj-&gt;_data, "00:00:00", "23:59:59");
  
  $obj -&gt; defineData( date("m/d/Y") );
  $obj -&gt; linha("_linha3", $obj-&gt;_data, "00:00:00", "23:59:59");
  
  $obj -&gt; gerarGrafico();
  </code></pre>
  
  <p>In this case above, the <code>$test</code> runs and prints me the result, but the <code>$obj</code> shows me the error below: </p>
  
  <pre><code>Fatal error: in C:\xampp\htdocs\classes\class.ConexaoPDO_PTA.inc.php on line 59
  </code></pre>
  
  <p>The line 59 in that file is the line below:</p>
  
  <pre><code>    public function execute(){
          return $this-&gt;stmt-&gt;execute();
      }
  </code></pre>
  
  <p>If I run the <code>$obj</code> separately, it runs fine, but not just after that one. What am I missing? Basically, if I wanna run N times the class <code>DadosGraph</code> I'll be not able to do so?
  Thank you all!</p>