PHP code example of linkorb / collection

1. Go to this page and download the library: Download linkorb/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/ */

    

linkorb / collection example snippets


$monkeys = new \Collection\TypedArray(Monkey::class);

$monkeys->add(new Monkey('Bubbles')); // OK
$monkeys[] = new Monkey('King Kong'); // OK
$monkeys->add(new Snake('Kaa')); // throws CollectionException

// You can iterate over the collection
foreach ($monkeys as $monkey) {
    echo $monkey->getName();
}

$monkeys[] = new Monkey('King Kong');

$monkeys->add(new Monkey('King Kong'));

class Monkey implements \Collection\Identifiable
{
    protected $name;
    public function __construct($name)
    {
        $this->name = $name;
    }

    public function identifier()
    {
        return $this->name;
    }
}

$monkeys = new \Collection\TypedArray(Monkey::class);

$m1 = new Monkey('George');
$m2 = new Monkey('Koko');

$monkeys[] = $m1; // array contains 1 item
$monkeys[] = $m2; // array contains 2 item
$monkeys[] = $m2; // array still contains 2 items!
$monkeys->add($m2); // array still contains 2 items!

$monkeys->hasKey('George')); // returns true
$monkeys->hasKey('King Kong')); // returns false
isset($monkeys['George']); // returns true

class Zoo
{
    protected $monkeys;

    public function __construct()
    {
        $this->monkeys = new Collection\TypedArray(Monkey::class);
    }

    public function getMonkeys()
    {
        return $this->monkeys;
    }
}

$zoo = new Zoo();
$zoo->getMonkeys()->add(new Monkey('Koko');

foreach ($zoo->getMonkeys() as $monkey() {
}