PHP code example of tandiljuan / collection

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

    

tandiljuan / collection example snippets




class MyCustomCollection extends \Tandiljuan\Collection\AbstractCollection
{
    /**
     * {@inheritdoc}
     */
    protected $itemType = '\DateTime';

    /**
     * {@inheritdoc}
     */
    protected $offsetType = 'integer';
}



$collection = new MyCustomCollection();

try {
    $collection[] = 'error';
} catch (\Tandiljuan\Collection\Exception\InvalidItemType $e) {
    echo "Invalid item type\n";
}

try {
    $collection['a'] = new \DateTime();
} catch (\Tandiljuan\Collection\Exception\InvalidOffsetType $e) {
    echo "Invalid offset type\n";
}

$collection[] = new DateTime('2001-01-01 01:01:01');
$collection[] = new DateTime('2002-02-02 02:02:02');
$collection[] = new DateTime('2003-03-03 03:03:03');



function printItems(MyCustomCollection $parameter)
{
    foreach ($parameter as $item) {
        echo $item->format('r')."\n";
    }
}

printItems($collection);

// The output will be
// Mon, 01 Jan 2001 01:01:01 +0000
// Sat, 02 Feb 2002 02:02:02 +0000
// Mon, 03 Mar 2003 03:03:03 +0000
bash
# Check if a file needs to be fixed
./run php-cs-fixer fix --dry-run --diff path/to/file.php
# Fix a file
./run php-cs-fixer fix path/to/file.php
bash
# Using PHP
./run php -a
# Using psysh
./run psysh