PHP code example of jojo1981 / typed-collection

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

    

jojo1981 / typed-collection example snippets




eate an empty collection of type `string`
$collection1 = new \Jojo1981\TypedCollection\Collection('string');

// Create a collection of type `integer` with some elements
$collection2 = new \Jojo1981\TypedCollection\Collection('int', [1, 2, 3]);

// Will throw an exception, because an invalid type has been given
try {
    $collection = new \Jojo1981\TypedCollection\Collection('invalid');
} catch (\Jojo1981\TypedCollection\Exception\CollectionException $exception) {
  echo $exception->getMessage() . PHP_EOL;
}

// Create a collection of class type \stdClass
$collection3 = new \Jojo1981\TypedCollection\Collection(\stdClass::class);
$collection3->pushElement(new \stdClass());

try {
    $collection3->pushElement('element');
} catch (\Jojo1981\TypedCollection\Exception\CollectionException $exception) {
    echo $exception->getMessage() . PHP_EOL;
}

echo 'Collection count: ' . $collection3->count() . PHP_EOL; // Will be 1