thinkphp5.x之Collection(集合)解析 php集合

本文涉及的产品
云解析DNS-重点域名监控,免费拨测 20万次(价值200元)
简介: 国庆节放假了,出去了发现所有地方全是人。 怕你们了,还是找个咖啡厅,静静的看着你们玩耍。 TP5也出来一段时间了,Let’s Go 看看新框架优点。 注释难免有不足之处,欢迎指正 thinkphp5.x之数据库操作相关解析 Db类 http://blog.csdn.net/fenglailea/article/details/52728705 thinkphp5 数

国庆节放假了,出去了发现所有地方全是人。
怕你们了,还是找个咖啡厅,静静的看着你们玩耍。
TP5也出来一段时间了,Let’s Go 看看新框架优点。
注释难免有不足之处,欢迎指正
thinkphp5.x之数据库操作相关解析 Db类
http://blog.csdn.net/fenglailea/article/details/52728705
thinkphp5 数据库 链接
http://blog.csdn.net/fenglailea/article/details/52728899
Collection 是集合的意思,也可以叫数据集/数据集合/对象
fox.风

//命名空间 这没什么好说的
//如果你还不知道这是什么,请先看 http://blog.csdn.net/fenglailea/article/details/52717646
namespace think;
// 数组式访问接口,php预定义接口 http://php.net/manual/zh/class.arrayaccess.php
use ArrayAccess;
// 数组迭代器 http://php.net/manual/zh/class.arrayiterator.php
use ArrayIterator;
// 统计元素对象个数接口 http://php.net/manual/zh/class.countable.php
use Countable;
// 聚合式迭代器接口,php预定义接口  http://php.net/manual/zh/class.iteratoraggregate.php
use IteratorAggregate;
// JSON序列话接口 http://php.net/manual/zh/class.jsonserializable.php
use JsonSerializable;
// 类 Collection 实现了ArrayAccess, Countable, IteratorAggregate, JsonSerializable接口
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{
    // 数据存储变量
    protected $items = [];
    // 构造函数
    public function __construct($items = [])
    {
        // 转化成数组,并赋值给变量items
        $this->items = $this->convertToArray($items);
    }
    // 装载/生成/制作等等,怎么叫法都可以,就是生成一个新结果集合
    public static function make($items = [])
    {
        // new static即是获取最终调用时的类[后续有介绍]
        return new static($items);
    }

    /**
     * 是否为空
     * @return bool
     */
    public function isEmpty()
    {
        return empty($this->items);
    }
    // 返回数组
    public function toArray()
    {
        // array_map :数组里的每个元素都经过这个回调函数处理
        return array_map(
            // 这是一个匿名回调函数
            function ($value) {
            return (
            // instanceof 检测变量$value是否属于Model类的实例
            $value instanceof Model || 
            // instanceof 检测变量$value是否属于当前 Collection类的实例
            $value instanceof self) ? 
            // 返回数组
            $value->toArray() : $value;
        }, $this->items);
    }
    // 返回变量 items 所有数据集
    public function all()
    {
        return $this->items;
    }

    /**
     * 合并数组
     *
     * @param  mixed $items
     * @return static
     */
    public function merge($items)
    {
        // new static即是获取最终调用时的类[后续有介绍]
        return new static(
        // array_merge 合并数组
        array_merge($this->items, 
        // 转换成数组
        $this->convertToArray($items)));
    }

    /**
     * 比较数组,返回差集
     *
     * @param  mixed $items
     * @return static
     */
    public function diff($items)
    {
        return new static(
        // 数组之间的差集
        array_diff($this->items, $this->convertToArray($items)));
    }

    /**
     * 交换数组中的键和值
     *
     * @return static
     */
    public function flip()
    {
        return new static(
        //  array_flip 交换数组中的键和值
        array_flip($this->items));
    }

    /**
     * 比较数组,返回交集
     *
     * @param  mixed $items
     * @return static
     */
    public function intersect($items)
    {
        return new static(
        // array_intersect 比较数组,返回交集
        array_intersect($this->items, $this->convertToArray($items)));
    }

    /**
     * 返回数组中所有的键名
     *
     * @return static
     */
    public function keys()
    {
        return new static(
        // array_keys 返回数组中所有的键名
        array_keys($this->items));
    }

    /**
     * 删除数组的最后一个元素(出栈)
     *
     * @return mixed
     */
    public function pop()
    {
        // 删除数组的最后一个元素(出栈)
        return array_pop($this->items);
    }

    /**
     * 通过使用用户自定义函数,以字符串返回数组
     *
     * @param  callable $callback
     * @param  mixed    $initial
     * @return mixed
     */
    public function reduce(callable $callback, $initial = null)
    {
        // array_reduce 用回调函数迭代地将数组简化为单一的值
        return array_reduce($this->items, $callback, $initial);
    }

    /**
     * 以相反的顺序返回数组。
     *
     * @return static
     */
    public function reverse()
    {
        return new static(
        // array_reverse 以相反的顺序返回数组
        array_reverse($this->items));
    }

    /**
     * 删除数组中首个元素,并返回被删除元素的值
     *
     * @return mixed
     */
    public function shift()
    {
        // array_shift 删除数组中首个元素,并返回被删除元素的值
        return array_shift($this->items);
    }

    /**
     * 把一个数组分割为新的数组块.
     *
     * @param  int  $size
     * @param  bool $preserveKeys
     * @return static
     */
    public function chunk($size, $preserveKeys = false)
    {
        $chunks = [];
        //array_chunk 把一个数组分割为新的数组块.
        foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) {
            $chunks[] = new static($chunk);
        }

        return new static($chunks);
    }

    /**
     * 在数组开头插入一个元素
     * @param mixed $value
     * @param null  $key
     * @return int
     */
    public function unshift($value, $key = null)
    {
        // 检测 $key 是否为null
        if (is_null($key)) {
            // array_unshift 在数组开头插入一个元素
            array_unshift($this->items, $value);
        } else {
            //两个数组组合成为新的数组,并赋值给items
            $this->items = [$key => $value] + $this->items;
        }
    }

    /**
     * 给每个元素执行个回调
     *
     * @param  callable $callback
     * @return $this
     */
    public function each(callable $callback)
    {
        //数组循环
        foreach ($this->items as $key => $item) {
            // 数组回调,如果不明白的,看PHP基础吧
            if ($callback($item, $key) === false) {
                break;
            }
        }
        //返回 当前对象
        return $this;
    }

    /**
     * 用回调函数过滤数组中的元素
     * @param callable|null $callback
     * @return static
     */
    public function filter(callable $callback = null)
    {
        if ($callback) {
            return new static(
            // array_filter用回调函数过滤数组中的元素
            array_filter($this->items, $callback));
        }

        return new static(
        // array_filter用回调函数过滤数组中的元素
        array_filter($this->items));
    }

    /**
     * 返回数组中指定的一列
     * @param      $column_key
     * @param null $index_key
     * @return array
     */
    public function column($column_key, $index_key = null)
    {
        // 检测函数是否存在,如果存在,则使用PHP内置函数处理,不存在则使用我们自己实现的方法
        if (function_exists('array_column')) {
            // array_column 返回数组中指定的列
            return array_column($this->items, $column_key, $index_key);
        }

        $result = [];
        foreach ($this->items as $row) {
            $key    = $value = null;
            $keySet = $valueSet = false;
            // array_key_exists 检查给定的键名或索引$index_key是否存在于数组$row中
            if (null !== $index_key && array_key_exists($index_key, $row)) {
                //如果存在,设置变量$keySet值为真
                $keySet = true;
                // 
                $key    = (string)$row[$index_key];
            }
            if (null === $column_key) {
                $valueSet = true;
                $value    = $row;
            } elseif (is_array($row) && array_key_exists($column_key, $row)) {
                $valueSet = true;
                $value    = $row[$column_key];
            }
            if ($valueSet) {
                if ($keySet) {
                    $result[$key] = $value;
                } else {
                    $result[] = $value;
                }
            }
        }
        return $result;
    }

    /**
     * 对数组排序
     *
     * @param  callable|null $callback
     * @return static
     */
    public function sort(callable $callback = null)
    {
        $items = $this->items;

        $callback ? 
        // uasort 使用用户自定义的$callback比较函数对$items数组中的值进行排序并保持索引关联
        uasort($items, $callback) : uasort($items, 
            // 这是个匿名函数
            function ($a, $b) {
            // 检测两个变量的值是否相等,相等返回0
            if ($a == $b) {
                return 0;
            }
            // 如果变量$a小于$b时,返回-1,否则都返回1
            return ($a < $b) ? -1 : 1;
        });

        return new static($items);
    }

    /**
     * 将数组打乱
     *
     * @return static
     */
    public function shuffle()
    {
        $items = $this->items;
        // 将数组打乱
        shuffle($items);

        return new static($items);
    }

    /**
     * 截取数组
     *
     * @param  int  $offset
     * @param  int  $length
     * @param  bool $preserveKeys
     * @return static
     */
    public function slice($offset, $length = null, $preserveKeys = false)
    {
        return new static(
        // array_slice 截取  $this->items 数组中 $offset 和 $length 之间的数组
        // $preserveKeys 默认会重新排序并重置数组的数字索引
        array_slice($this->items, $offset, $length, $preserveKeys));
    }

    // ArrayAccess
    public function offsetExists($offset)
    {
        // 检查给定的$offset键名或索引是否存在于$this->items数组中
        return array_key_exists($offset, $this->items);
    }

    public function offsetGet($offset)
    {
        //返回 $this->items数组中指定 $offset键名或索引 的数据或者对象
        return $this->items[$offset];
    }
    // 数组 中增加一条新数据或者对象
    public function offsetSet($offset, $value)
    {
        if (is_null($offset)) {
            $this->items[] = $value;
        } else {
            $this->items[$offset] = $value;
        }
    }
    // 删除指定 $offset键名或索引 的数据或者对象
    public function offsetUnset($offset)
    {
        unset($this->items[$offset]);
    }

    //Countable
    public function count()
    {
        // 返回 计算数组中的单元数目或对象中的属性个数
        return count($this->items);
    }

    //IteratorAggregate
    public function getIterator()
    {
        // 返回 创建外部迭代器
        return new ArrayIterator($this->items);
    }

    //JsonSerializable
    public function jsonSerialize()
    {
        // 返回序列化数组
        return $this->toArray();
    }

    /**
     * 转换当前数据集为JSON字符串
     * @access public
     * @param integer $options json参数
     * @return string
     */
    public function toJson($options = JSON_UNESCAPED_UNICODE)
    {
        // 返回JSON 格式数据
        return json_encode($this->toArray(), $options);
    }

    public function __toString()
    {
        // 返回JSON 格式数据
        return $this->toJson();
    }

    /**
     * 转换成数组
     *
     * @param  mixed $items
     * @return array
     */
    protected function convertToArray($items)
    {
        // instanceof 检测变量$items是否属于当前类Collection的实例
        if ($items instanceof self) {
            // 执行all方法,返回所有
            return $items->all();
        }
        // 强制转换为数组
        return (array)$items;
    }
}

new self 和 new static(后期静态绑定 )

self 取决于当前方法定义的类
static 即是获取最终调用时的类(即当前实例化的那个类)

class A{
    public static function testSelf() {
        return new self();
    }
    public static function testStatic() {
        return new static();
    }
}
class B extends A{}
echo get_class(B::testSelf());  /* A */
echo get_class(B::testStatic());  /* B  */

输出
A
B

制作自己的 PHP Collection类

看看上面的源码,只要删除2处($value instanceof Model ),命名空间名字再改一下,就是自己 PHP Collection集合类,是不是非常棒~
使用方式

$data 就是数据库取出的数据
$result=new Collection($data);
目录
相关文章
|
6月前
|
JSON 数据处理 PHP
PHP数组处理技巧:高效操作数据集合
PHP数组处理技巧:高效操作数据集合
|
6月前
|
JSON 定位技术 PHP
PHP技巧:解析JSON及提取数据
这就是在PHP世界里探索JSON数据的艺术。这场狩猎不仅仅是为了获得数据,而是一种透彻理解数据结构的行动,让数据在你的编码海洋中畅游。通过这次冒险,你已经掌握了打开数据宝箱的钥匙。紧握它,让你在编程世界中随心所欲地航行。
226 67
|
6月前
|
运维 监控 算法
局域网屏幕监控软件 PHP 图像块增量传输算法解析
本文探讨了一种基于PHP语言开发的图像块增量传输算法,适用于局域网屏幕监控场景。通过将屏幕图像分块处理、计算哈希值并对比变化区域,该算法显著降低了网络带宽占用,提升了监控效率。在企业管理和远程教育中,该技术可实现终端设备的实时监控与远程管控,同时支持与生物识别等技术融合,拓展应用范围。实验表明,该算法在常规办公场景下可减少90%以上的数据传输量,展现了良好的实时性和优化效果。
130 3
|
7月前
|
存储 监控 算法
内网监控桌面与 PHP 哈希算法:从数据追踪到行为审计的技术解析
本文探讨了内网监控桌面系统的技术需求与数据结构选型,重点分析了哈希算法在企业内网安全管理中的应用。通过PHP语言实现的SHA-256算法,可有效支持软件准入控制、数据传输审计及操作日志存证等功能。文章还介绍了性能优化策略(如分块哈希计算和并行处理)与安全增强措施(如盐值强化和动态更新),并展望了哈希算法在图像处理、网络流量分析等领域的扩展应用。最终强调了构建完整内网安全闭环的重要性,为企业数字资产保护提供技术支撑。
213 2
|
9月前
|
存储 监控 算法
关于员工上网监控系统中 PHP 关联数组算法的学术解析
在当代企业管理中,员工上网监控系统是维护信息安全和提升工作效率的关键工具。PHP 中的关联数组凭借其灵活的键值对存储方式,在记录员工网络活动、管理访问规则及分析上网行为等方面发挥重要作用。通过关联数组,系统能高效记录每位员工的上网历史,设定网站访问权限,并统计不同类型的网站访问频率,帮助企业洞察员工上网模式,发现潜在问题并采取相应管理措施,从而保障信息安全和提高工作效率。
169 7
|
PHP 开发者 容器
PHP命名空间深度解析:避免命名冲突与提升代码组织####
本文深入探讨了PHP中命名空间的概念、用途及最佳实践,揭示其在解决全局命名冲突、提高代码可维护性方面的重要性。通过生动实例和详尽分析,本文将帮助开发者有效利用命名空间来优化大型项目结构,确保代码的清晰与高效。 ####
181 20
|
运维 数据库连接 PHP
PHP中的异常处理机制深度解析####
本文深入探讨了PHP中异常处理机制的工作原理,通过实例分析展示了如何有效地使用try-catch语句来捕获和处理运行时错误。我们将从基础概念出发,逐步深入到高级应用技巧,旨在帮助开发者更好地理解和利用这一强大的工具,以提高代码的稳定性和可维护性。 ####
|
PHP 开发者 UED
PHP中的异常处理机制解析####
本文深入探讨了PHP中的异常处理机制,通过实例解析try-catch语句的用法,并对比传统错误处理方式,揭示其在提升代码健壮性与可维护性方面的优势。文章还简要介绍了自定义异常类的创建及其应用场景,为开发者提供实用的技术参考。 ####
|
PHP 开发者 容器
PHP命名空间深度解析及其最佳实践####
本文深入探讨了PHP中引入命名空间的重要性与实用性,通过实例讲解了如何定义、使用及别名化命名空间,旨在帮助开发者有效避免代码冲突,提升项目的模块化与可维护性。同时,文章还涉及了PHP-FIG标准,引导读者遵循最佳实践,优化代码结构,促进团队协作效率。 ####
169 1
|
9月前
|
算法 测试技术 C语言
深入理解HTTP/2:nghttp2库源码解析及客户端实现示例
通过解析nghttp2库的源码和实现一个简单的HTTP/2客户端示例,本文详细介绍了HTTP/2的关键特性和nghttp2的核心实现。了解这些内容可以帮助开发者更好地理解HTTP/2协议,提高Web应用的性能和用户体验。对于实际开发中的应用,可以根据需要进一步优化和扩展代码,以满足具体需求。
871 29

推荐镜像

更多
  • DNS