php 设计模式
1: php 工厂设计模式
'; return new $type; } else { echo 'driver not found'; throw new Exception('Driver not found'); } }}//使用工厂$a = Factory::fac('A');$a->method();$b = Factory::fac('B');$b->method();?>
2:php 单例设计模式
'; echo 'please do not create by yourself!'; } //单例方法 public static function singleton() { if(!isset(self::$instance)) { $theClass = __CLASS__; self::$instance = new $theClass; } return self::$instance; } //单例中的普通方法 public function hello() { echo 'hello everyone! I am singleton '; } //阻止用户复制对象实例 public function __clone() { trigger_error('do not clone the singleton.',E_USER_ERROR); }}//$test = new Single(); //错误调用//单例的正确使用方式;$sing = Single::singleton();$sing-> hello();//clone测试//$test = clone $sing; //会收到,上面的 clone错误;?>
3:json 数据处理
'jkk','age'=>22,'sex'=>'man','phone'=>1321058559); echo json_encode($arr);?>
4:数据库连接设计
server = $server; $this->user_name = $user_name; $this->password = $password; $this->db = $db; $this->connect(); } private function connect() { //这里面初始为 $this->link 为 数据库连接; echo "
"; echo $this->server.''; echo $this->user_name.''; echo $this->password.''; echo $this->db.''; } }$con = new Connection('ubuntuServer14','test','test','db');?>