php链式调用(链式操作)

简介: 2017年6月28日 10:41:19 星期三 情景: 在多次处理数组的时候, 要自定义好多个临时变量, 起名字特别麻烦 于是, 就想到利用PHP的 1.魔法方法__call 2.不定参数, 参数自动解包的特性 写了一个简单的链式操作类: 调用举例: 1 $arr = [ ...

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 }

 

目录
相关文章
|
1月前
|
PHP
php的foreach神操作
php的foreach神操作
22 0
|
7月前
|
关系型数据库 MySQL PHP
PHP 原生操作 Mysql
PHP 原生操作 Mysql
81 0
|
7月前
|
关系型数据库 MySQL 数据库连接
PHP 原生操作 Mysql 增删改查案例
PHP 原生操作 Mysql 增删改查案例
87 0
|
3月前
|
SQL PHP 数据库
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
PHP案例:每一个账号登陆后的操作是隔离的(使用token进行登录)
|
4月前
|
NoSQL PHP Redis
PHP 操作Redis
PHP 操作Redis
29 0
|
6月前
|
Unix PHP
PHP时间戳如何操作,以及如何拿到时间
PHP时间戳如何操作,以及如何拿到时间
|
7月前
|
关系型数据库 MySQL PHP
PHP 原生操作 Mysql 分页数据案例
PHP 原生操作 Mysql 分页数据案例
96 1
|
8月前
|
PHP
PHP 操作Redis
PHP 操作Redis
49 0
|
9月前
|
NoSQL 关系型数据库 MySQL
PHP快速入门13-MySQL数据库与Redis操作
PHP快速入门与实战,本篇文章介绍的内容,MySQL数据库与Redis操作,后续更多内容将收录在专栏PHP快速入门与实战。
|
9月前
|
SQL 缓存 关系型数据库
php开发实战分析(7):mysql类库操作的高级使用
php开发实战分析(7):mysql类库操作的高级使用
127 0