Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

Singleton Pattern Demo

Mon Oct 27, 2008 12:20 pm

Code:

<?php
  
class Configuration {
     static
private $instance = NULL;
     
private $settingsArray;
  
     
private function __construct(){
     }
  
     
public function __destruct() {
        if(!
$this->updated) {
           return;
        }

        foreach (
$this->settingsArray as $key => $value) {
          echo(
"$key = \"$value\"\n");
        }

     }
  
     
public function getInstance() {
        if(
self::$instance == NULL) {
          
self::$instance = new Configuration();
        }
        return
self::$instance;
     }
  
     
public function get($name) {
        if(isset(
$this->settingsArray[$name])) {
           return
$this->settingsArray[$name];
        } else {
           return(
NULL);
        }
     }
  
     
public function set($name, $value) {
       if(!isset(
$this->settingsArray[$name]) OR ($this->settingsArray[$name] != $value)) {
          
$this->settingsArray[$name] = $value;
          
$this->updated = TRUE;
       }
     }
  }

  
$config = Configuration::getInstance();
  
$config->set("username", "A");
  
$config->set("password", "B");
  print(
$config->get("username"));
?>




Post a reply
  Related Posts  to : Singleton Pattern Demo
 The Singleton Pattern     -  
 Sharing Passcert 1Y0-A04 exam demo     -  
 Bluetooth demo in WTK25 does not work     -  
 Java seminar topic with demo     -  
 DAO Pattern     -  
 DAO design pattern     -  
 Factory pattern     -  
 about dao design pattern     -  
 Strategy Pattern     -  
 Replacing a Pattern with a Found String     -  

Topic Tags

PHP OOP