承接 pogulailo/collection 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

pogulailo/collection

最新稳定版本:v1.0.0

Composer 安装命令:

composer require pogulailo/collection

包简介

Strictly typed collection

README 文档

README

A simple and concise implementation of strictly typed arrays in PHP. No extra code, just type-checked arrays.

Usage

First you need to create your own collection class which extends the GenericCollection and sets its type:

use Pogulailo\Collection\GenericCollection;

class CustomerCollection extends GenericCollection
{
    public function __construct(...$values)
    {
        parent::__construct(Customer::class, ...$values);
    }
}

That's all, then you can enjoy all the advantages of strictly typed arrays:

function getCustomers(): CustomerCollection
{
    $customers = new CustomerCollection();
    
    $customers->append(new Customer());
    $customers->append(new Customer());
    $customers->append(new Customer());
    
    return $customers;
}

function doSomething(CustomerCollection $customers): void
{
    foreach ($customers as $customer) {
        // Do what you need to do
    }
}

$customers = getCustomers();
doSomething($customers);

You can choose not to create your own collection class, but then you will need to do additional type checking:

use Pogulailo\Collection\GenericCollection;

function getCustomers(): GenericCollection
{
    $customers = new GenericCollection(Customer::class);
    
    $customers->append(new Customer());
    $customers->append(new Customer());
    $customers->append(new Customer());
    
    return $customers;
}

function doSomething(GenericCollection $customers): void
{
    // In this case, you need to check the collection type first
    if ($customers->getType() !== Customer::class) {
        throw new Exception('I need customers, more customers...')
    }
    
    foreach ($customers as $customer) {
        // Do what you need to do
    }
}

$customers = getCustomers();
doSomething($customers);

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-09-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固