chenlong пре 4 година
родитељ
комит
f23c3e1b65
14 измењених фајлова са 447 додато и 6 уклоњено
  1. 79 0
      Core/AllUser.php
  2. 59 4
      Core/Database.php
  3. 29 0
      Core/Databases/Mysql.php
  4. 28 0
      Core/Databases/Mysqli.php
  5. 28 0
      Core/Databases/PDO.php
  6. 19 0
      Core/EventGenerator.php
  7. 16 0
      Core/Factory.php
  8. 12 0
      Core/IDatabase.php
  9. 2 1
      Core/Loader.php
  10. 6 0
      Core/Observer.php
  11. 36 0
      Core/Register.php
  12. 33 0
      Core/User.php
  13. 98 1
      index.php
  14. 2 0
      note.txt

+ 79 - 0
Core/AllUser.php

@@ -0,0 +1,79 @@
+<?php
+namespace Core;
+/**
+ * 迭代器模式
+ * Class AllUser
+ * @package Core
+ */
+class AllUser implements \Iterator
+{
+    /**
+     * @var 迭代器的当前位置
+     */
+    protected $index;
+
+    /**
+     * @var array|mixed
+     */
+    protected $ids = array();
+
+    /**
+     * @var Databases\Mysqli
+     */
+    protected $user;
+
+    public function __construct()
+    {
+        $this->user = new \Core\User();
+        $this->ids = $this->user->getIds();
+    }
+
+    /**
+     * 第三步:拿到当前数据
+     * @return mixed|void
+     */
+    public function current()
+    {
+        // TODO: Implement current() method.
+        $id = $this->ids[$this->index]['id'];
+        return $this->user->getUserById($id);
+    }
+
+    /**
+     * 迭代器索引增幅
+     */
+    public function next()
+    {
+        // TODO: Implement next() method.
+        $this->index ++;
+    }
+
+    /**
+     * @return bool|迭代器的当前位置|float|int|string|null
+     */
+    public function key()
+    {
+        // TODO: Implement key() method.
+        return $this->index;
+    }
+
+    /**
+     * 第一步:将索引标记在开头
+     */
+    public function rewind()
+    {
+        // TODO: Implement rewind() method.
+        $this->index = 0;
+    }
+
+    /**
+     * 第二步:验证是否还有下一个元素
+     * @return bool|void
+     */
+    public function valid()
+    {
+        // TODO: Implement valid() method.
+        return $this->index < count($this->ids);
+    }
+
+}

+ 59 - 4
Core/Database.php

@@ -1,14 +1,69 @@
 <?php
 namespace Core;
-
+/**
+ * 单利模式
+ * Class Database
+ * @package Core
+ */
 class Database{
-    function where($where){
+    /**
+     * 单例模式
+     * @var
+     */
+    protected static $db = null;
+    /**
+     * 私有化
+     */
+    private function __construct(){
+
+    }
+
+    /**
+     * 私有化
+     */
+    private function __clone(){
+
+    }
+
+    /**
+     * 私有化
+     * 当在类外部使用unserialize()时会调用这里的__wakeup()方法
+     */
+    private function __wakeup(){
+
+    }
+
+    /**
+     * 获取实例
+     */
+    static function getInstance(){
+        if(is_null(self::$db)){
+            self::$db = new self();
+        }
+        return self::$db;
+    }
+
+    /**
+     * @param $where
+     * @return $this
+     */
+    public function where($where){
         return $this;
     }
-    function order($order){
+
+    /**
+     * @param $order
+     * @return $this
+     */
+    public function order($order){
         return $this;
     }
-    function limit($limit){
+
+    /**
+     * @param $limit
+     * @return $this
+     */
+    public function limit($limit){
         return $this;
     }
 }

+ 29 - 0
Core/Databases/Mysql.php

@@ -0,0 +1,29 @@
+<?php
+namespace Core\Databases;
+
+use Core\IDatabase;
+
+class Mysql implements IDatabase{
+
+    protected $conn;
+
+    public function connect($host,$user,$passwd,$dbname)
+    {
+        // TODO: Implement connect() method.
+        $conn = mysql_connect($host,$user,$passwd);
+        mysql_connect_db($dbname,$conn);
+        $this->conn = $conn;
+    }
+
+    public function query($query)
+    {
+        // TODO: Implement query() method.
+        return mysql_query($query,$this->conn);
+    }
+
+    public function close()
+    {
+        // TODO: Implement close() method.
+        mysql_close($this->conn);
+    }
+}

+ 28 - 0
Core/Databases/Mysqli.php

@@ -0,0 +1,28 @@
+<?php
+namespace Core\Databases;
+
+use Core\IDatabase;
+
+class Mysqli implements IDatabase{
+
+    protected $conn;
+
+    public function connect($host,$user,$passwd,$dbname)
+    {
+        // TODO: Implement connect() method.
+        $conn = mysqli_connect($host,$user,$passwd,$dbname);
+        $this->conn = $conn;
+    }
+
+    public function query($query)
+    {
+        // TODO: Implement query() method.
+        return mysqli_query($this->conn,$query);
+    }
+
+    public function close()
+    {
+        // TODO: Implement close() method.
+        mysqli_close($this->conn);
+    }
+}

+ 28 - 0
Core/Databases/PDO.php

@@ -0,0 +1,28 @@
+<?php
+namespace Core\Databases;
+
+use Core\IDatabase;
+
+class PDO implements IDatabase{
+
+    protected $conn;
+
+    public function connect($host,$user,$passwd,$dbname)
+    {
+        // TODO: Implement connect() method.
+        $conn = new \PDO("mysql:host=$host;dbname=$dbname",$user,$passwd);
+        $this->conn = $conn;
+    }
+
+    public function query($query)
+    {
+        // TODO: Implement query() method.
+        return $this->conn->query($query);
+    }
+
+    public function close()
+    {
+        // TODO: Implement close() method.
+        unset($this->conn);
+    }
+}

+ 19 - 0
Core/EventGenerator.php

@@ -0,0 +1,19 @@
+<?php
+namespace Core;
+
+abstract class EventGenerator
+{
+    protected $observers = array();
+
+    function addObserver($observer)
+    {
+        $this->observers[] = $observer;
+    }
+    function notify()
+    {
+        foreach($this->observers as $observer)
+        {
+            $observer->update();
+        }
+    }
+}

+ 16 - 0
Core/Factory.php

@@ -0,0 +1,16 @@
+<?php
+namespace Core;
+/**
+ * 工厂模式
+ * Class Factory
+ * @package Core
+ */
+class Factory{
+    /**
+     * 创建数据库连接实例
+     * @return Database
+     */
+    static function createDatabase(){
+        return Database::getInstance();
+    }
+}

+ 12 - 0
Core/IDatabase.php

@@ -0,0 +1,12 @@
+<?php
+namespace Core;
+/**
+ * 适配器模式--例如定义数据库操作接口
+ * Interface IDatabase
+ * @package Core
+ */
+interface IDatabase{
+    function connect($host,$user,$passwd,$dbname);
+    function query($query);
+    function close();
+}

+ 2 - 1
Core/Loader.php

@@ -5,7 +5,8 @@ class Loader{
     /**
      * @param $class
      */
-    static function autoload($class){
+    static function autoload($class)
+    {
         /**
          * $class  出来的是反斜杠 App\Controller\Home\Index
          * 需要转化一下 再包含进来

+ 6 - 0
Core/Observer.php

@@ -0,0 +1,6 @@
+<?php
+namespace Core;
+
+interface Observer{
+    public function update($event_info = null);
+}

+ 36 - 0
Core/Register.php

@@ -0,0 +1,36 @@
+<?php
+namespace Core;
+/**
+ * 注册树模式
+ * Class Register
+ * @package Core
+ */
+class Register{
+    /**
+     * @var array
+     */
+    protected static $objects = array();
+
+    /**
+     * @param $alias
+     * @param $object
+     */
+    public function set($alias,$object){
+        self::$objects[$alias] = $object;
+    }
+
+    /**
+     * @param $alias
+     * @return mixed
+     */
+    public function get($alias){
+        return self::$objects[$alias];
+    }
+
+    /**
+     * @param $alias
+     */
+    public function remove($alias){
+        unset(self::$objects[$alias]);
+    }
+}

+ 33 - 0
Core/User.php

@@ -0,0 +1,33 @@
+<?php
+namespace Core;
+
+class User{
+    protected $db;
+
+    public function __construct()
+    {
+        $this->db = new \Core\Databases\Mysqli();
+        $this->db->connect('39.100.75.63:33060', 'root', 'mysql57-2020!d', 'prictice');
+    }
+
+    public function getUserById($id)
+    {
+        $res = $this->db->query("select * from test where id=".$id)->fetch_all(MYSQLI_ASSOC)[0];
+        $this->id = $res['id'];
+        $this->name = $res['name'];
+        $this->mobile = $res['mobile'];
+        $this->regtime = $res['regtime'];
+        return $this;
+    }
+
+    public function save()
+    {
+        $sql = "update test set name = '{$this->name}',mobile = '{$this->mobile}',regtime = '{$this->regtime}' where id = '{$this->id}'";
+        $this->db->query($sql);
+    }
+
+    public function getIds()
+    {
+        return $this->db->query("select id from test")->fetch_all(MYSQLI_ASSOC);
+    }
+}

+ 98 - 1
index.php

@@ -19,4 +19,101 @@ spl_autoload_register('\\Core\\Loader::autoload');
  * 面向对象--链式操作
  */
 //$db = new Core\Database();
-//$db->where()->order()->limit();
+//$db->where()->order()->limit();
+
+/**
+ * 工厂模式
+ */
+//$db = Core\Factory::createDatabase();
+//$db->where()->order()->limit();
+
+/**
+ * 注册树模式
+ */
+//Core\Register::set('db',Core\Database::getInstance()); //这里只是演示,实际上需要在初始化时就要注册。
+//$db = Core\Register::get('db');
+//$db->where()->order()->limit();
+
+/**
+ * 适配器模式
+ * desc 定义一个接口
+ */
+//$db = new Core\Databases\Mysql();
+//db->connect('39.100.75.63:33060','root','mysql57-2020!d','prictice');
+//$db->query('sql');
+//$db->close();
+
+/**
+ * 策略模式
+ * desc 通过动态参数调用的方式--控制反转。同适配器模式。
+ */
+
+/**
+ * 数据对象映射模式
+ */
+//$user = new Core\User();
+//$user->getUserById(1);
+//$user->name = 'chen';
+//$user->mobile = '13676666666';
+//$user->regtime = date('Y-m-d H:i:s');
+//$user->save();
+
+/**
+ * 观察者模式
+ */
+//class event extends Core\EventGenerator
+//{
+//    public function trigger(){
+//        $this->notify();
+//    }
+//}
+//class Observer1 implements \Core\Observer
+//{
+//    function update($event_info = null)
+//    {
+//        // TODO: Implement update() method.
+//        echo '观察者1';
+//    }
+//}
+//class Observer2 implements \Core\Observer
+//{
+//    function update($event_info = null)
+//    {
+//        // TODO: Implement update() method.
+//        echo '观察者2';
+//    }
+//}
+//$event = new event();
+//$event->addObserver(new Observer1());
+//$event->addObserver(new Observer2());
+//$event->trigger();
+
+/**
+ * 原型模式
+ * 通过clone方法,将初始化的工作只做一次,节省开销
+ */
+
+/**
+ * 装饰器模式
+ * 可以动态添加修改类的功能
+ * https://www.cnblogs.com/onephp/p/6108940.html
+ */
+
+/**
+ * 迭代器模式
+ * 在不需要了解内部实现情况下,遍历一个聚合对象的内部元素
+ * 需要继承Iterater
+ */
+//$allUser = new Core\AllUser();
+//foreach ($allUser as $user){
+//    var_dump($user->mobile);
+//    $user->name = 'lisi';
+//    $user->save();
+//}
+
+/**
+ * 代理模式
+ * 相当于把功能委派给代理对象来判断执行
+ */
+
+

+ 2 - 0
note.txt

@@ -9,3 +9,5 @@ php链式操作
     __callStatic
     __toString  //将对象当字符串使用
     __invoke    //将对象当函数使用
+    __wakeup    //当在类外部使用unserialize()时会调用这里的__wakeup()方法
+    __sleep     //当在类外部使用serialize()时会调用这里的__sleep()方法