美文网首页数据库
面向对象-链式函数写法与使用

面向对象-链式函数写法与使用

作者: 云龙789 | 来源:发表于2017-12-18 18:53 被阅读9次

举例

$obj = new Obj();
$name = $obj->table('test')->where('id','>','3')->get(['id','time']);

链式函数其实是每次执行这个函数的时候,
1.给这个函数中的属性赋值,
2.返回本类return $this

下面的一个类是模仿类似于laravel DB类的一种写法

<?php
class Obj
{
    private $sql_connect;
    private $where = '';

    /**
     * Obj constructor. 我这里是用了直接连接好的mysql变量
     */
    public function __construct()
    {
        global $sql_connect;
        $this->sql_connect = $sql_connect;
    }

    public  function table($table)
    {
        $this->table = $table;
        return $this;
    }
    public function where($k='',$gt='=',$v='')
    {
        // 如果有传递参数,则给$this->>where 赋值 ,
        if ($k){
            if (!$v){
                $this->where = "where $k = $gt";
            }else{
                $this->where = "where $k $gt $v";
            }
        }
        // 否则,就不用做处理,因为有 where属性默认值是空  where=''
        return $this;
    }

    public function get($val='*')
    {
        // 如果是数组,则拆分,不是数组则取值全部
        if ($val && is_array($val)){
            $val = implode(',',$val);
        }
        $res = $this->le_db1->query("select $val from $this->table $this->where");
        // 去掉数字的键
        $array = $res->fetchAll();
        foreach ($array as $key=>$value){
            foreach ($value as $k => $val){
                if (is_numeric($k)){
                    unset($array[$key][$k]);
                }
            }
        }
        return $array;
    }
    public function insert($arr)
    {
        // 判断是一维还是二维数组
        if (count($arr) == count($arr,1)){
            // 一维数组
            $keys = array_keys($arr);
            $from = '('.implode(',',$keys).')';
            $to = '(\''.implode('\',\'',$arr).'\')';
        }else{
            // 二维数组  取出第一组数组中的键就是要插入数据的键
            $keys = array_keys($arr[0]);
            $to = '';
            $from = '('.implode(',',$keys).')';
            foreach ($arr as $key => $value){
                $to = $to.'(\''.implode('\',\'',$value).'\'),';
            }
            $to = rtrim($to,',');
        }
        $this->le_db1->query("insert into $this->table $from VALUES $to");
        // 返回最后一个id
        return $this->le_db1->lastInsertId(); 
    }
    public function update($arr)
    {
        $set = '';
        foreach ($arr as $key=>$value){
          // 如果是字符串,要添加引号
            if (is_string($value)){
                $value = "'$value'";
            }
            $set = $set.$key.'='.$value.',';
        }
        $set = rtrim($set,',');
        $res = $this->le_db1->query("update $this->table set $set $this->where");
        return $res->rowCount();
    }

    public function delete()
    {
        // 防止删除全部表,此处可以做where限制
        if (!$this->where){
            return '请谨慎操作,不要删除全部表数据';
        }
        $res = $this->le_db1->query("delete  from $this->table  $this->where");
        return $res->rowCount();
    }
}

注意事项

  • 方法中的参数最好要写默认值,否则使用的时候如果没有传递值,是会报错的,比如$where = '';

  • 使用方式

  1. 获取id>3'id','time'字段
$obj = new Obj();
$res = $obj->table('test')->where('id','>','3')->get(['id','time']);
echo '<pre>';
print_r($res);

2.获取全部数据

$obj = new Obj();
$res = $obj->table('test')->where()->get();
或者
$res = $obj->table('test')->get();

插入数据的使用

$obj = new Obj();
$res = $obj->table('test')->insert([['test'=>'4','time'=>'4'],['test'=>'5','time'=>'5'],['test'=>'6','time'=>'6']]);
$res = $obj->table('test')->insert(['test'=>'4','time'=>'4']);

删除
$res = $obj->table('test')->where('id',2)->delete();
更新
$res = $obj->table('test')->where('id',3)->update(['test'=>4]);

相关文章

  • 面向对象-链式函数写法与使用

    举例 链式函数其实是每次执行这个函数的时候,1.给这个函数中的属性赋值,2.返回本类return $this 下面...

  • 数组去重(两种方法)

    数组去重(面向对象 && 链式写法)

  • 第十九章 Class的基本语法

    简介   Javascript语言的传统方法是通过构造函数定义并生成新对象。   上面这种写法与传统的面向对象语言...

  • JS面向对象及组件开发课件

    什么是面向对象 面向对象编程的特点 面向对象的基本写法和组成 工厂方式和构造函数 对象引用是什么和它的问题 ale...

  • ES6之Class跟普通构造函数

    构造函数 Class构造函数 class的本质还是函数 Class 继承 总结 class更加贴近于面向对象的写法...

  • JS原型的面试考点

    class与普通构造函数的区别 class实际上是函数,是语法糖 class在语法上更贴近面向对象的写法。 cla...

  • vue常用的ES6语法--class

    新增加的语法糖,作用:让对象原型的写法更加清晰,更像面向对象的编程方式;构造函数的另一种写法1、构造函数 2、通过...

  • Class

    ES6创建对象的方法,可以看作语法糖,它的本质是函数。让对象原型的写法更清晰,更加像面向对象的语法。 Class与...

  • jQuery中的deferred对象

    deferred对象简单来说就是jQuery的回调函数解决方案。 一、ajax中的链式写法 jQuery中ajax...

  • Scala面试题

    scala语言的特点: 集成了面向对象和函数式编程 函数式编程是将计算机的运算视为函数运算 链式编程 Scala中...

网友评论

    本文标题:面向对象-链式函数写法与使用

    本文链接:https://www.haomeiwen.com/subject/xtnmwxtx.html