1. Go to this page and download the library: Download darkdevlab/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/ */
darkdevlab / collection example snippets
use DarkDevLab\Collection\Collection;
class User
{
private $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
/**
* @property User[] $container
* @method User current()
*/
class UserCollection extends Collection
{
public function addItem(User $user): self
{
return parent::add($user);
}
}
$collection = new UserCollection();
$collection->addItem(new User('John'));
$collection->addItem(new User('John')); // Only one added. Duplication will be skipped
$collection->addItem(new User('Mark'));
foreach ($collection as $user) {
echo sprintf('Username: %s', $user->getName()), PHP_EOL;
}
use DarkDevLab\Collection\GeneratorCollection;
class UserCollection extends GeneratorCollection
{
}
class UserRepository
{
private $pdo;
public function findAllByName(string $name): UserCollection
{
$stmt = $this->pdo->prepare('SELECT * FROM `user` WHERE name=:name');
$generator = function () use ($stmt) {
if ($stmt->execute()) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_NEXT)) {
if (!empty($row)) {
yield new User($row['name']);
}
}
} else {
yield;
}
};
return new UserCollection($generator());
}
}
$userRepository = new UserRepository(/* PDO */);
$userCollection = $userRepository->findAllByName('John');
foreach ($userCollection as $user) {
echo sprintf('Username: %s', $user->getName()), PHP_EOL;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.