• 工作大概2年多了,一提魔术常量都知道。但是在很多情况下,我们只知道某个函数的用法以及传参,但是不知道其应用场景。
  • 今天写一个小的应用场景,不多废话,直接上代码。

    <?php
    /**
     * Created by ZhengNiu.
     * User: 77103
     * Date: 2019/4/4
     * Time: 15:19
     */
    
    namespace app\helper;
    
    class db
    {
      private $sql = [
          'table' => '',
          'field' => '*',
          'where' => '',
          'order' => '',
          'limit' => ''
      ];
      protected $where = '';
      protected $order = '';
      protected $limit = '';
    
      public function __call($name, $arguments)
      {
          if (array_key_exists($name, $this->sql)) {
              $this->sql[$name] = $arguments[0];
          }
          return $this;
      }
    
      public function select()
      {
          if (!empty($this->sql['where'])) {
              $this->where = ' where ' . $this->sql['where'];
          }
          if (!empty($this->sql['order'])) {
              $this->order = ' order by ' . $this->sql['order'];
          }
          if (!empty($this->sql['limit'])) {
              $this->limit = ' limit ' . $this->sql['limit'];
          }
          $sql = 'select ' . $this->sql['field'] . ' from ' . $this->sql['table'] . $this->where . $this->order . $this->limit;
          echo $sql;
      }
    }
    // 调用
    $db = new db();
    $db->table('user')->field('id,username,email,age')->where("email like '%qq.com%'")->order('id desc')->limit('1,10')->select();
    Logic::vd($db);

    请输入图片描述

Last modification:February 25, 2022
如果觉得我的文章对你有用,请随意赞赏