• php链式调用(链式操作)


    2017年6月28日 10:41:19 星期三

    情景:

    在多次处理数组的时候, 要自定义好多个临时变量, 起名字特别麻烦

    于是, 就想到利用PHP的

    1.魔法方法__call

    2.不定参数, 参数自动解包的特性

    写了一个简单的链式操作类:

    调用举例:

     1 $arr = [
     2     ['id' => 1, 'name' =>'111'],
     3     ['id' => 2, 'name' =>'222'],
     4     ['id' => 3, 'name' =>'333'],
     5     ['id' => 4, 'name' =>'333'],
     6     ['id' => 5, 'name' =>''],
     7 ];
     8 
     9 echo Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique(); // {"1":"111","2":"222","3":"333"} 调用__toString
    10 Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->count()->strval()->echo(); // 3
    11 Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->reset()->var_dump(); // string(3) "111"
    12 Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->end()->echo(); // 333
    13 var_dump(Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->empty()); //bool(false)
    14 echo Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->get(1); //111
    15 echo Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->get(5); //0
    16 Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->json_encode()->exit(); // {"1":"111","2":"222","3":"333"}
    17 Data::ini($arr)->array_column('name', 'id')->array_filter()->array_unique()->implode(',')->echo();
    18 Data::ini('aaa^111|bbb^222|ccc^333')->explode('|')->json_encode()->echo();

     代码文件: 

     1 <?php
     2 
     3 /**
     4  * 此类的本意是链式调用PHP自带函数
     5  * 除了本类中自带的函数, 调用其它函数时需要该函数的第一个原始参数是待处理的数据($this->data)
     6  * 本类中有些函数名跟PHP自带函数一样, 需要php7以上支持(PHP7优化了词法分析器, 可以做出区分)
     7  */
     8 class Data
     9 {
    10     public $data = null;
    11     
    12     public function __construct($data)
    13     {
    14         $this->data = $data;
    15     }
    16     
    17     //没有起名为getInstance或init是因为 "ini"三个字母可以只用右手就可以敲出来
    18     public static function ini($data)
    19     {
    20         return new Data($data);
    21     }
    22     
    23     /*
    24      * 核心代码
    25      * 利用可变函数(变量函数)去隐式调用PHP自带函数
    26      * 可变函数不能用于例如 echo,print,unset(),isset(),empty(),include,require 以及类似的语言结构。需要使用自己的包装函数来将这些结构用作可变函数。
    27      * http://www.php.net/manual/zh/functions.variable-functions.php
    28      */
    29     public function __call($func, $args) 
    30     {
    31         $this->data = $func($this->data, ...$args);
    32         return $this;
    33     }
    34     
    35     public function get($key, $default='0')
    36     {
    37         // return $this->data[$key] ?? $default;
    38         return isset($this->data[$key]) ? $this->data[$key] : $default;
    39     }
    40     
    41     public function group($key)
    42     {
    43         $new = array();
    44         foreach ($this->data as $v) {
    45             $new[$v[$key]][] = $v;
    46         }
    47         $this->data = $new;
    48         return $this;
    49     }
    50     
    51     public function implode($char='')
    52     {
    53         $this->data = implode($char, $this->data);
    54         return $this;
    55     }
    56     
    57     public function explode($char)
    58     {
    59         $this->data = explode($char, $this->data);
    60         return $this;
    61     }
    62     
    63     public function empty()
    64     {
    65         return empty($this->data);
    66     }
    67     
    68     public function echo()
    69     {
    70         echo $this->data;//参数为整型时会被当作状态码返回 e.g. 200/404/500.... 浏览器无输出
    71     }
    72     
    73     public function exit()
    74     {
    75         exit($this->data); //参数为整型时会被当作状态码返回 e.g. 200/404/500.... 浏览器无输出
    76     }
    77     
    78     //echo exit 时默认调用此函数
    79     public function __toString()
    80     {
    81         return json_encode($this->data);
    82     }
    83     
    84 }
  • 相关阅读:
    kendoUI行编辑器的使用grid.editRow($("#grid tr:eq(1)"))无效
    Kendo-UI学习 DataSource 数据源属性说明
    fineReport 下拉联动 js
    报表FineReport中单元格中各种颜色的标识说明
    keil DSP最新版本
    ESP8266固件烧录篇
    git 报错 时出现Clone failed early EOF错误解决
    STM32 HAL库、标准外设库、LL库(STM32 Embedded Software)
    再谈EPLAN 中的项目结构-帮助理解
    启动EPLAN时,应该选哪个版本?Compact/select start/professional/select/maintenance/professional+/ultimate
  • 原文地址:https://www.cnblogs.com/iLoveMyD/p/7088685.html
Copyright © 2020-2023  润新知