PHP code example of bapcat / collection

1. Go to this page and download the library: Download bapcat/collection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bapcat / collection example snippets


$collection = new Collection(['k1' => 'v1']);

var_dump($collection->get('k1')); // 'v1'
var_dump($collection->size()); // 1

$collection->set('k2', 'v2');

$collection->add('v3');

// $collection == ['k1' => 'v1', 'k2' => 'v2', 'v3']

$collection->remove('k2');

// $collection == ['k1' => 'v1', 'v3']

foreach($collection as $k => $v) {
    //
}

class MyCollection implements ReadableCollectionInterface {
    use ReadableCollectionTrait;
    
    protected $collection = [];
}

class MyCollection extends Collection {
    //
}

class MyCollection implements ReadableCollectionInterface, WritableCollectionInterface {
    use ReadableCollectionTrait, WritableCollectionTrait;
}

$collection->lazy(1, function($key) {
    return User::find($key);
});