定制 brezgalov/php-typed-collection 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

brezgalov/php-typed-collection

Composer 安装命令:

composer require brezgalov/php-typed-collection

包简介

base class for making typed collections via PHP iterator

README 文档

README

composer require brezgalov/php-typed-collection dev-master

Description

Abstraction designed to make typed collections based on Iterator interface

If you need more functions - feel fre to add them to your custom implementation. You can implement ArrayAccess when it required

Usage

class MyClassCollection extends AbstractTypedIterator
{
    /**
     * override to set collection element type 
     */
    public function current(): MyClass
    {
        return parent::current();
    }

    /**
     * Make shure items are same type 
     */
    protected function validateItem($item): bool
    {
        return $item instanceof MyClass;
    }
}

// now go use

$collection = new MyClassCollection([
    new MyClass(),
    new MyClass(),
    new MyClass(),
]);

foreach($collection as $class) {
    // $class instance of MyClass - put your handle logic here 
}

Deep cloning

Objects stored inside collection are references.

If you want to clone collection safely - consider using ContainerDeepCloneTrait or AbstractTypedCloneSafeIterator as it shown below:

ContainerDeepCloneTrait:

class MyClassCollection extends AbstractTypedIterator
{
    use ContainerDeepCloneTrait;

    public function current(): MyClass
    {
        return parent::current();
    }

    protected function validateItem($item): bool
    {
        return $item instanceof MyClass;
    }
}

$clone = clone $myClassCollection; // it's deep cloned now

AbstractTypedCloneSafeIterator:

class MyClassCollection extends AbstractTypedCloneSafeIterator
{
    ...
}

function testClone() {
    $myClass = new MyClass();
    $array = [$myClass];

    return new MyClassCollection($array); // $myClass is cloned
}

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-01-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固