PHP可以';不要多次调用类方法
作为一个PHP OOP类的初学者,我需要一些帮助来理解我做错了什么,以及我可以做得更好 我的数据库连接有以下代码:PHP可以';不要多次调用类方法,php,class,oop,pdo,Php,Class,Oop,Pdo,作为一个PHP OOP类的初学者,我需要一些帮助来理解我做错了什么,以及我可以做得更好 我的数据库连接有以下代码: 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_PAS
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->host_pdo_pta.'; Database='.$this->dbname_pdo_pta.';';
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
try
{
$this->dbh = new PDO($dsn, $this->user_pdo_pta, $this->pass_pdo_pta, $options);
}
catch(PDOException $e)
{
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->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->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function fetchAllAssoc(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function fetchAssoc(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
public function fetchAllObj(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function fetchObj(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}
public function rowCount(){
return count($this->stmt->fetchAll());
}
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
public function closeConnection(){
return $this->dbh = null;
}
}
那很好用。
然后我试着用这个来生成一个图形数据:
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->_visao = "VS1";
break;
case 'vs2':
$this->_visao = "VS2";
break;
case 'vs3':
$this->_visao = "VS3";
break;
case 'vs4':
$this->_visao = "VS4";
break;
default:
$this->_visao = null;
break;
}
}
public function defineData($varData)
{
list($mes, $dia, $ano) = explode("/", $varData);
$this->_exibirData = $dia."/".$mes;
$this->_data = $mes."/".$dia."/".$ano;
$this->_diaMes = $dia."/".$mes;
$this->_mesAno = $mes."/".$ano;
}
public function defineTipoEmpresa($TipoEmpresa = null, $Empresa = null)
{
if(($TipoEmpresa != null) AND ($Empresa != null)):
$this->_tipoEmpresa = ", @".$TipoEmpresa." = '".$Empresa."'";
else:
$this->_tipoEmpresa = null;
endif;
}
public function gerarGrafico()
{
$arrTotal = array_replace_recursive($this->_linha1, $this->_linha2, $this->_linha3);
$result = array();
foreach ($arrTotal as $key => $value) {
$horario = $value['Hora'].":59";
if( $value['Hora'] >= 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.' },<br>';
}
}
public function linha($linha, $data, $horaInicial, $horaFinal)
{
$this->_dataInicial = $data." ".$horaInicial;
$this->_dataFinal = $data." ".$horaFinal;
$this->query("execute spBilhetesEmitidosporEmpresa @Acao = '".$this->_visao."', @DataHoraVenda = '".$this->_dataInicial."', @DataHoraVendaFinal = '".$this->_dataFinal."' ".$this->_tipoEmpresa."");
$this->execute();
$SelectDiaSPReceita = $this->fetchAllAssoc();
$ord = array();
foreach ($SelectDiaSPReceita as $value){
$ord[] = strtotime( $value['DataHoraVenda'] );
}
array_multisort($ord, SORT_ASC, $SelectDiaSPReceita);
$i = 0;
$arrChavesHorarios = array();
while ($i < 2400) {
$a = sprintf("%04d", $i);
$hora = substr( $a, 0, 2);
$horario = $hora.":00";
$arrChavesHorarios[] = $horario;
$i = $i + 100;
}
$i = 0;
$arrN = array();
while ($i < 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" => $dia, "Mes" => $mes, "Ano" => $ano, "Hora" => $hora, "BilhetesVendidos" => 1, "TarifaTotal" => 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" => 0, "Mes" => 0, "Ano" => 0, "Hora" => $hora, "BilhetesVendidos" => 0, "TarifaTotal" => 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 &$entry) {
$bilhetesVendidos += $entry['BilhetesVendidos'];
$tarifaTotal += $entry['TarifaTotal'];
$entry['BilhetesVendidos'] = $bilhetesVendidos;
$entry['TarifaTotal'] = $tarifaTotal;
$arrTotalSP[] = array("Dia" => $entry["Dia"], "Mes" => $entry["Mes"], "Ano" => $entry["Ano"], "Hora" => $entry["Hora"], "BilhetesVendidos".$linha => $entry["BilhetesVendidos"], "TarifaTotal".$linha => number_format( $entry["TarifaTotal"] , 2, ".", "") );
}
$this->$linha = $arrTotalSP;
}
}
该文件中的第59行如下:
如果我单独运行$obj
,它运行得很好,但不只是在那之后。我错过了什么?基本上,如果我想运行N次类DadosGraph
,我就不能这样做了?
谢谢大家! 您的设计中有一个巨大的错误:DadosGraph不应该继承ConexaoPDO_PTA 通常,OOP-CRUD是以一种根本不同的方式完成的:
现在你在做这件事,好像DadosGraph是一种ConexaoPDO_PTA,这是错误的。所以,基本上我一直在做这件事。。。哈哈,谢谢你的提示,我知道我得多读一些。我承认,我认为“extends”的意思类似于“放在一起”以便更容易地运行两者。您还可以将它们分开,初始化连接,并将其实例作为DadosGraph类@Edson的参数传递
error_reporting(E_ALL);
require_once("../classes/class.autoload.inc.php");
echo "<pre>Up and running!</pre><br><br>";
$test = new DadosGraph;
$test -> visao("vs3");
$test -> defineTipoEmpresa();
$test -> defineData("10/05/2015");
$test -> linha("_linha1", $test->_data, "00:00:00", "23:59:59");
$test -> defineData("10/16/2015");
$test -> linha("_linha2", $test->_data, "00:00:00", "23:59:59");
$test -> defineData( date("m/d/Y") );
$test -> linha("_linha3", $test->_data, "00:00:00", "23:59:59");
$test -> gerarGrafico();
echo "<br><br>";
$obj = new DadosGraph;
$obj -> visao("vs3");
$obj -> defineTipoEmpresa("Empresa", "nome_emp");
$obj -> defineData("10/05/2015");
$obj -> linha("_linha1", $obj->_data, "00:00:00", "23:59:59");
$obj -> defineData("10/16/2015");
$obj -> linha("_linha2", $obj->_data, "00:00:00", "23:59:59");
$obj -> defineData( date("m/d/Y") );
$obj -> linha("_linha3", $obj->_data, "00:00:00", "23:59:59");
$obj -> gerarGrafico();
Fatal error: in C:\xampp\htdocs\classes\class.ConexaoPDO_PTA.inc.php on line 59
public function execute(){
return $this->stmt->execute();
}