phpÊý¾Ý¿â²Ù×÷modelÀà(ʹÓÃ__call·½·¨)

5年以前  |  阅读数:914 次  |  编程语言:PHP 

±¾ÎÄʵÀý½²ÊoÁËphpÊý¾Ý¿a²Ù×÷modelÀa¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º

¸ÃÊý¾Ý¿a²Ù×÷ÀaʹÓÃ__call()½¨ÊµÏÖÁËÊý¾ÝµÄ²eÕÒ¹¦ÄÜ¡£

´uÂeÈçÏ£º


    <?php
    /*
    ×÷Õß : shyhero
    */
    define("HOSTNAME","127.0.0.1");
    define("USERNAME","root");
    define("PASSWORD","");
    define("DATANAME","class");
    class Model{
        private $link;
        private $tableName;
        private $zd;
        private $method = array(
          "where" => "",
          "order" => "",
          "limit" => "",
          "group" => "",
          "having" => ""
          );
        public function __construct($tableName){
          $this -> tableName = $tableName;
          try{
            $this -> link = mysqli_connect(HOSTNAME,USERNAME,PASSWORD,DATANAME);
            mysqli_set_charset($this -> link,"UTF8");
          }catch(Exception $e){
            echo "Êý¾Ý¿aÁ¬½Óʧ°Ü";
          }
          $this -> desc();
        }
        public function __destruct(){
          mysqli_close($this -> link);
        }
        public function desc(){
          $sql = " desc {$this -> tableName}; ";
          $res = mysqli_query($this -> link,$sql);
          $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
          for($i = 0 ;$i < count($arr);$i++){
            $brr[] = $arr[$i]['Field'];
          }
          $this -> zd = $brr;
          return $brr;
        }
        public function __call($name,$value){
          $name = strtolower($name);
          if(array_key_exists($name,$this -> method)){
            if($name == 'order'){
              $this -> method['order'] = " order by ".$value[0];
            }elseif($name == 'group'){
            $this -> method['group'] = " group by ".$value[0];
            }else{
              $this -> method[$name] = " {$name} ".$value[0];
            }
          }else{
            return "the method is not found!";
          }
          return $this;
        }
        public function method(){
          return " {$this -> method['where']} {$this -> method['order']} {$this -> method['limit']} {$this -> method['group']} {$this -> method['having']}; ";
        }
        public function find($a="*"){
          if(in_array("{$a}",$this -> zd) || $a == "*"){
            $sql = "select {$a} from {$this -> tableName} {$this -> method()} ";
          }else{
            $sql = "select * from {$this -> tableName}";
          }
          //return $sql;
          $res = mysqli_query($this -> link,$sql);
          $arr = mysqli_fetch_all($res,MYSQLI_ASSOC);
          return $arr;
        }
    }

ÓÃ*¨Ê¾Àý£º


    <?php
      function __autoload($className){
        require($className.".class.php");
      }
      $a = new Model("stu");
      $a -> where("name = 'zhu'")->limit("5,10");
      var_dump($a -> find("name"));

¸u¶a¹ØÓÚPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¡¢¡¶PHP»uÓÚpdo²Ù×÷Êý¾Ý¿a¼¼ÇÉ×ܽa¡¡¢¡¶PHP+MongoDBÊý¾Ý¿a²Ù×÷¼¼ÇÉ´oÈ«¡¡¢¡¶php+OracleÊý¾Ý¿a³ÌÐoÉe¼Æ¼¼ÇÉ×ܽa¡¡¢¡¶php+mssqlÊý¾Ý¿a³ÌÐoÉe¼Æ¼¼ÇÉ×ܽa¡¡¢¡¶php+redisÊý¾Ý¿a³ÌÐoÉe¼Æ¼¼ÇÉ×ܽa¡¡¢¡¶php+mysqliÊý¾Ý¿a³ÌÐoÉe¼Æ¼¼ÇÉ×ܽa¡¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡

Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例