單例模式

<?php

class Preferences {
    private $props = array();
    private static $instance;

    private function __construct()
    {
    }

    public static function getInstance(){
        if(empty(self::$instance)){
            self::$instance = new Preferences();
        }
        return self::$instance;
    }

    public function setProperty($key, $val){
        $this->props[$key] = $val;
    }

    public function getProperty($key){
        return $this->props[$key];
    }

}

$pref = Preferences::getInstance();
$pref->setProperty("name","matt");

unset($pref);

$pref2 = Preferences::getInstance();
print $pref2->getProperty("name")."\n";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章