PHP 数据对象映射

主入口

define('BASEDIR',__DIR__);
include BASEDIR.'/Common/Loader.php';
spl_autoload_register('\\Common\\Loader::autoload');  //SPL自动载入
use App\Factory;

//数据对象映射模式 + 工厂模式 + 单例模式 + 注册器模式
class page{
    function name(){
        //$user=Factory::new_getUser(1);//工厂模式
        $user=Factory::getUser(1);//工厂模式+注册器
        $user->name='abc';
    }
    function mobile(){
        //$user=Factory::new_getUser(1);//工厂模式
        //这里和上面函数就new了2次user,浪费资源。第一次user后应放到注册树上,来减少消耗
        $user=Factory::getUser(1);//工厂模式+注册器
        $user->mobile='133123';
    }
}

$page=new page;
$page->name();
$page->mobile();

工厂类

namespace App;
//工厂模式
class Factory{
    //非注册器模式,new多次浪费
    static function new_getUser($id){
            $user = new User($id);
            return $user;
    }
    //注册器模式
    static function getUser($id){
        $key = 'user_'.$id;
        $user = Register::get($key);
        if (!$user) {
            $user = new User($id);
            Register::set($key, $user);
        }
        return $user;
    }
    static function createDatabase()    {
        $db=Database::getInstance();
        Register::set('db1',$db);
        return $db;
    }
}

注册器类

namespace App;
//注册器模式
class Register{
    protected static $objects;

    static function set($alias, $object)    {
        self::$objects[$alias] = $object;
    }
    static function get($key)    {
        if (!isset(self::$objects[$key]))        {
            return false;
        }
        return self::$objects[$key];
    }
    function _unset($alias)    {
        unset(self::$objects[$alias]);
    }
}

数据对象映射类

namespace App;
use App\Database\MySQLi;
class User{
    protected $id;
    protected $db;

    function __construct($id)    {
        $this->db = new MySQLi();  //在这里轻松切换PDO和MySQL,下面的代码不变仍能达到一致的结果
        $this->db -> connect('127.0.0.1','root','123456','test');
        $res=$this->db->query("select * from user where id = $id limit 1");
        $data=$res->fetch_assoc();
        $this->id=$data['id'];
        $this->name=$data['name'];
        $this->mobile=$data['mobile'];
        $this->create_time=$data['create_time'];
    }
    //执行到php? >就会自动执行析构函数
    function __destruct()    {
        $this->db->query("update user set name='{$this->name}',mobile='{$this->mobile}',
          create_time='{$this->create_time}' where id = {$this->id} limit 1");
    }
}

sql文件

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `mobile` varchar(255) NOT NULL,
  `create_time` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `user` (`id`, `name`, `mobile`, `create_time`) VALUES
(1, 'abc', '13812341234', '2019-02-16'),
(2, 'test', '13300001111', '2019-02-16');
ALTER TABLE `user`
  ADD PRIMARY KEY (`id`);
ALTER TABLE `user`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;

Database类

namespace App;

class Database{
    static private $db;
    private function __construct()    {
    }
    //单例模式
    static function getInstance()    {
        if (empty(self::$db)) {
            self::$db = new self;
            return self::$db;
        } else {
            return self::$db;
        }
    }
    function where($where)    {
        return $this;
    }
    function order($order)    {
        return $this;
    }
    function limit($limit)    {
        return $this;
    }
    function query($sql)    {
        echo "SQL: $sql\n";
    }
}

你可能感兴趣的:(PHP 数据对象映射)