PHP设计模式之工厂模式与单例模式

yipeiwu_com5年前PHP代码库

本文实例讲述了PHP设计模式之工厂模式与单例模式实现方法。分享给大家供大家参考,具体如下:

设计模式简单说应对某类问题而设计的解决方式

工厂模式:应对需求创建相应的对象

class factory{
  function __construct($name){
    if(file_exists('./'.$name.'.class.php')){
      return new $name;
    }else{
      die('not exist');
    }
  }
}

单例模式:只创建一个对象的实例,不允许再创建实例,节约资源(例如数据库的连接)

class instance{
  public $val = 10;
  private static $instance ;
  private function __construct(){}
  private function __clone(){}
  //设置为静态方法才可被类调用
  public static function getInstance(){
    /*if(!isset(self::$instance)){
      self::$instance = new self;
    }*/
    if(!isset(instance::$instance)){
      instance::$instance = new self;
    }
    return instance::$instance;
  }
}
$obj_one = instance::getInstance();
$obj_one->val = 20;
//clone可以调用__clone()克隆即new出一个新的的对象
//$obj_two = clone $obj_one;
$obj_two = instance::getInstance();
echo $obj_two->val;
echo '<p>';
var_dump($obj_one,$obj_two);

运行结果如下:

20
object(instance)[1]
 public 'val' => int 20
object(instance)[1]
 public 'val' => int 20

应用:数据库连接类(database access oject)

class mysqldb{
  private $arr = array(
    'port' => 3306,
    'host' => 'localhost',
    'username' => 'root',
    'passward' => 'root',
    'dbname' => 'instance',
    'charset' => 'utf8'
     );
  private $link;
  static $instance;
  private function __clone(){}
  private function __construct(){
    $this->link = mysql_connect($this->arr['host'],$this->arr['username'],$this->arr['passward']) or die(mysql_error());
    mysql_select_db($this->arr['dbname']) or die('db error');
    mysql_set_charset($this->arr['charset']);
  }
  static public function getInsance(){
    if(!isset(mysqldb::$instance)){
      mysqldb::$instance = new self;
    }
    return mysqldb::$instance;
  }
  public function query($sql){
    if($res = mysql_query($sql)){
      return $res;
    }return false;
  }
  //fetch one
  public function get_one($sql){
    $res = $this->query($sql);
    if($result = mysql_fetch_row($res)){
      return $result[0];
    }
  }
  //fetch row
  public function get_row($sql){
    $res = $this->query($sql);
    if($result = mysql_fetch_assoc($res)){
      return $result;
    }
    return false;
  }
  //fetch all
  public function get_all($sql){
    $res = $this->query($sql);
    $arr = array();
    while($result = mysql_fetch_assoc($res)){
      $arr[] = $result;
    }
    return $arr;
  }
}
$mysql = mysqldb::getInsance();

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关文章

PHP使用Redis替代文件存储Session的方法

PHP使用Redis替代文件存储Session的方法

本文实例讲述了PHP使用Redis替代文件存储Session的方法。分享给大家供大家参考,具体如下: PHP默认使用文件存储session,如果并发量大,效率非常低。而Redis对高并发...

php网页病毒清除类

本文实例讲述了php网页病毒清除类。分享给大家供大家参考。具体如下: 相信很多人的网页经常被无故的在php,asp,html,js 等文件后台加上一些木马地址,造成了很大的困扰!我以前有...

9段PHP实用功能的代码推荐

一、查看邮件是否已被阅读   当你在发送邮件时,你或许很想知道该邮件是否被对方已阅读。这里有段非常有趣的代码片段能够显示对方IP地址记录阅读的实际日期和时间。 复制代码 代码如下: &...

php取得字符串首字母的方法

本文实例讲述了php取得字符串首字母的方法。分享给大家供大家参考。具体实现方法如下: <?php $limit = array( //gb2312 拼音排序 arr...

PHP解压tar.gz格式文件的方法

PHP解压tar.gz格式文件的方法

本文实例讲述了PHP解压tar.gz格式文件的方法。分享给大家供大家参考,具体如下: 1、运用php自带压缩与归档扩展(phar) $phar = new PharData('son...