<!-- wp:paragraph -->
<p>工作大概2年多了,一提魔术常量都知道。但是在很多情况下,我们只知道某个函数的用法以及传参,但是不知道其应用场景。</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>今天写一个小的应用场景,不多废话,直接上代码。</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

<?php
/**
 * Created by ZhengNiu.
 * User: 77103
 * Date: 2019/4/4
 * Time: 15:19
 */

namespace apphelper;

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;
    }
}

<!-- /wp:code -->

<!-- wp:paragraph -->
<p>调用</p>
<!-- /wp:paragraph -->

<!-- wp:code -->

 $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);

<!-- /wp:code -->

<!-- wp:image {"id":70} -->
<figure class="wp-block-image"></figure>
<!-- /wp:image -->

关注友儿不迷路

Last modification:July 16th, 2019 at 02:08 pm
如果觉得我的文章对你有用,请随意赞赏