maxalmonte14 / phpcollections
PHP的一系列集合。
v1.3.0
2018-11-18 15:57 UTC
Requires
- php: >=7.2
Requires (Dev)
- evert/phpdoc-md: ^0.2.0
- phpunit/phpunit: ^7.4
This package is auto-updated.
Last update: 2024-09-19 01:17:51 UTC
README
关于PHPCollections
PHPCollections 是一组旨在使您在处理 PHP 和大量数据时更加轻松的数据结构。受到 Java 或 C# 等语言的影响,PHPCollections 提供了列表、映射、栈等数据结构,查看一下吧!
要求
PHP >= 7.2
安装
composer require "maxalmonte14/phpcollections"
示例
想象一下,您正在如此存储用于检索的 Post 对象。
$posts[] = new Post(1, 'PHP 7.2 release notes'); $posts[] = new Post(2, 'New Laravel 5.5 LTS make:factory command');
一切正常!但也许由于某种神秘原因,您犯了一个错误,并添加了一个非 Post 对象。
$posts[] = 5 // This is not even an object!
当您尝试检索您的帖子数组时,您会遇到麻烦。
<?php foreach($posts as $post): ?> <tr> <!-- this gonna fail when $post == 5! --> <td><?= $post->id; ?></td> <td><?= $post->title; ?></td> </tr> <?php endforeach ?>
幸运的是,PHPCollections 存在。
$posts = new GenericList( Post::class, new Post(1, 'PHP 7.2 release notes'), new Post(2, 'New Laravel 5.5 LTS make:factory command') ); $posts->add(5); // An InvalidArgumentException is thrown!
当然,存在更多灵活的数据结构,如 ArrayList。
$posts = new ArrayList(); $posts->add(new Post(1, 'PHP 7.2 release notes')); $posts->add(new Post(2, 'New Laravel 5.5 LTS make:factory command')); $posts->add(5); // Everything is fine, I need this 5 anyway
特性
- 不同类型的集合,如字典、栈和泛型列表。
- 简单的 API。
- 轻量级,无需额外包。