programster / collections
A simple library to simplify the creation of strict type arrays/collections.
0.1.0
2023-12-18 15:15 UTC
Requires
- php: >=8.2.0
This package is not auto-updated.
Last update: 2024-11-11 15:21:47 UTC
README
A package to simplify the creation of strict type arrays in PHP.
Example Usage
The example below creates a collection of users and sorts them by their name.
<?php require_once(__DIR__ . '/vendor/autoload.php'); class User { public function __construct(public readonly string $name){} } class UserCollection extends \Programster\Collections\AbstractCollection { public function __construct(User ...$elements) { parent::__construct(User::class, ...$elements); } } $names = new UserCollection( new User("Programster"), new User("Bar"), new User("Foo"), ); // Demonstrate that can append a user, like with a normal array. $names[] = new User("Added User"); $sortByNameFunc = function(User $name1, User $name2){ return $name1->name <=> $name2->name; }; $names->uasort($sortByNameFunc); print "Users sorted by name: " . print_r($names, true) . PHP_EOL;