PHP code example of atournayre / collection

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

    

atournayre / collection example snippets


// Samples classes
class Person
{
    public function __construct(
        public string $name
    ) {}
}

class People extends TypedCollection
{
    protected static string $type = Person::class;
}

// Create collection
$collection = People::createAsList([
    new Person('John'),
]);
$collection[] = new Person('Jack'); // Add item

// Samples classes
class Person
{
    public function __construct(
        public string $name
    ) {}
}

class People extends TypedCollectionImmutable
{
    protected static string $type = Person::class;
}

// Create collection
$collection = People::createAsList([
    new Person('John'),
]);
$collection[] = new Person('Jack'); // Throws a RuntimeException

$collection = DecimalValueCollection::fromArray([
    DecimalValue::create(4.235, 3),
    DecimalValue::fromInt(1),
    DecimalValue::fromString('2'),
    DecimalValue::fromFloat(3.01, 2),
], 2);
$collection[0]->toFloat(); // 4.24
$collection[1]->toFloat(); // 1.00
$collection[2]->toFloat(); // 2.00
$collection[3]->toFloat(); // 3.01