PHP / OOP:记忆完成了吗?

时间:2014-02-10 10:59:06

标签: php oop memoization

我正在尝试在我的数据库字段上做Memoizaiton,我想我犯了一个错误,因为它不打印$fieldCache数组的结果。

我对PHP中的OOP很新,所以如果有人可以提供帮助并给我一些建议,我会很高兴。

这是我的班级代码:

 <?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);

1 个答案:

答案 0 :(得分:0)

您可以通过在PHP之前应用$fieldCache关键字来访问静态属性(例如self

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

self指的是当前的课程

并且无需global财产