2014-02-10 22 views
0

我想在我的數據庫字段上做Memoizaiton,我想我犯了一個錯誤,因爲它不打印$fieldCache數組的結果。PHP/OOP:Memoization做對了嗎?

我對PHP的面向對象很陌生,所以如果有人能夠幫助並給我一些建議,我會非常感激。

這裏是我的類代碼:

<?php 


    class Main{ 
     public $id; 
     public $maxT; 
     public $meanT; 
     public $minT; 
     public $dewP; 
     public $meanDP; 
     public $minDP; 
     public $maxH; 
     public $meanH; 
     public $minH; 
     public $maxSLP; 
     public $meanSLP; 
     public $minSLP; 
     public $maxV; 
     public $meanV; 
     public $minV; 
     public $maxWS; 
     public $meanWS; 
     public $maxGS; 
     public $prec; 
     public $cloudCover; 
     public $event; 
     public $windDir; 
     public static $fieldCache=array(); 

     public function getID($table,$date){ 
      return $this->_data('id',$table,$date); 
     } 

     public function getMaxT($table,$date){ 
      return $this->_data('MaxTemperatureC',$table,$date); 
     } 

     public function getMeanT($table,$date){ 
      return $this->_data('MeanTemperatureC',$table,$date); 
     } 

     public function getMinT($table,$date){ 
      return $this->_data('MinTemperatureC',$table,$date); 
     } 

     public function getDewP($table,$date){ 
      return $this->_data('DewPointC',$table,$date); 
     } 

     public function getMeanDewP($table,$date){ 
      return $this->_data('MeanDewPointC',$table,$date); 
     } 

     public function getMinDewP($table,$date){ 
      return $this->_data('MinDewPointC',$table,$date); 
     } 

     public function getMaxH($table,$date){ 
      return $this->_data('MaxHumidity',$table,$date); 
     } 

     public function getMeanH($table,$date){ 
      return $this->_data('MeanHumidity',$table,$date); 
     } 

     public function getMinH($table,$date){ 
      return $this->_data('MinHumidity',$table,$date); 
     } 

     public function getMaxSLP($table,$date){ 
      return $this->_data('MaxSeaLevelPressurehPa',$table,$date); 
     } 

     public function getMeanSLP($table,$date){ 
      return $this->_data('MeanSeaLevelPressurehPa',$table,$date); 
     } 

     public function getMinSLP($table,$date){ 
      return $this->_data('MinSeaLevelPressurehPa',$table,$date); 
     } 

     public function getMaxV($table,$date){ 
      return $this->_data('MaxVisibilityKm',$table,$date); 
     } 

     public function getMeanV($table,$date){ 
      return $this->_data('MeanVisibilityKm',$table,$date); 
     } 

     public function getMinV($table,$date){ 
      return $this->_data('MinVisibilityKm',$table,$date); 
     } 

     public function getMaxWS($table,$date){ 
      return $this->_data('MaxWindSpeedKmh',$table,$date); 
     } 

     public function getMeanWS($table,$date){ 
      return $this->_data('MeanWindSpeedKmh',$table,$date); 
     } 

     public function getMaxGS($table,$date){ 
      return $this->_data('MaxGustSpeedKmh',$table,$date); 
     } 

     public function getPrec($table,$date){ 
      return $this->_data('Precipitationmm',$table,$date); 
     } 

     public function getCloudCover($table,$date){ 
      return $this->_data('CloudCover',$table,$date); 
     } 

     public function getEvent($table,$date){ 
      return $this->_data('Events',$table,$date); 
     } 

     public function getWindDir($table,$date){ 
      return $this->_data('WindDirDegrees',$table,$date); 
     } 

     private function _data($field,$table,$date){ 

      global $fieldCache; 

      //echo ' field : '.$field." | table: ".$table." | date: ".$date."<br />"; 

      $dbh=new PDO("sqlite2:sqlite/dbTest1.db",null,null,array(PDO::ATTR_PERSISTENT=>true)) or die("<link rel='stylesheet' type='text/css' href='../css/style.css' title='Default' /> 
               <center><h3><span class='blue'>Oh my!</span><span class='yellow'> Our</span><span class='red'> database</span><span class='green'> is</span> 
               <span class='blue'>trying</span><span class='yellow'> to escape!</span> <span class='red'>We</span><span class='green'> have dispatched</span><span class='blue'> a team of highly koal-ified</span> 
               <span class='green'>koalas</span><span class='yellow'> to</span><span class='red'> catch it!</span> <span class='blue'>Please</span><span class='red'> try again</span><span class='green'> later</span><span class='yellow'>!</span> 
               <br /> 
               <img src='images/koala_running.gif'><center/><h3 />"); 

      $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

       //MemoizationCheck for stored results 
      if(isset(self::$fieldCache[$field][$table][$date])){ 
       echo self::$fieldCache[$field][$table][$date]; 
       return self::$fieldCache[$field][$table][$date]; 
      } 

      $query=$dbh->query("SELECT $field FROM $table WHERE Date=$date"); 
      $result=$query->fetch(PDO::FETCH_ASSOC); 
      self::$fieldCache[$field][$table][$date]=$result; 
       echo self::$fieldCache[$table][$date]; 
      return $result[$field]; 
       echo $result[$field]; 
      unset($dbh); 

     } 




    } 
?> 

而且從我的其他文件中設置:

$obj=new Main(); 
    $id=$obj->getID($Country,$datepicker); 
    $maxT=$obj->getMaxT($Country,$datepicker); 
    $meanT=$obj->getMeanT($Country,$datepicker); 
    $minT=$obj->getMinT($Country,$datepicker); 
    $dewP=$obj->getDewP($Country,$datepicker); 
    $meanDP=$obj->getMeanDewP($Country,$datepicker); 
    $minDP=$obj->getMinDewP($Country,$datepicker); 
    $maxH=$obj->getMaxH($Country,$datepicker); 
    $meanH=$obj->getMeanH($Country,$datepicker); 
    $minH=$obj->getMinH($Country,$datepicker); 
    $maxSLP=$obj->getMaxSLP($Country,$datepicker); 
    $meanSLP=$obj->getMeanSLP($Country,$datepicker); 
    $minSLP=$obj->getMinSLP($Country,$datepicker); 
    $maxV=$obj->getMaxV($Country,$datepicker); 
    $meanV=$obj->getMeanV($Country,$datepicker); 
    $minV=$obj->getMinV($Country,$datepicker); 
    $maxWS=$obj->getMaxWS($Country,$datepicker); 
    $meanWS=$obj->getMeanWS($Country,$datepicker); 
    $maxGS=$obj->getMaxGS($Country,$datepicker); 
    $prec=$obj->getPrec($Country,$datepicker); 
    $cloudCover=$obj->getCloudCover($Country,$datepicker); 
    $event=$obj->getEvent($Country,$datepicker); 
    $windDir=$obj->getWindDir($Country,$datepicker); 

回答

0

您可以通過應用self關鍵字之前,他們在PHP訪問靜態屬性(如$fieldCache

if(isset(self::$fieldCache[$field][$table][$date])){ 
    return self::$fieldCache[$field][$table][$date]; 
} 

self指當前類

,無需global財產

+0

有了您的建議的方法仍然經歷,但沒有仍是通過'回聲$ fieldCache [$場] [$表] [$日期]所示;' – braumer

+0

在哪裏你迴應它? – zzlalani

+0

如果你在類中使用它自己做,那麼用類名'Main :: $ fieldCache [$ field] [$ table] [$ date]'' – zzlalani