PHP code example of programster / collections
1. Go to this page and download the library: Download programster/collections 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/ */
programster / collections example snippets
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;