1. Go to this page and download the library: Download stratadox/php-generics 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/ */
stratadox / php-generics example snippets
use Stratadox\PhpGenerics\Autoloader;
Autoloader::default()->install();
namespace My\Name\Space;
use My\Generic\Collection;
var_dump(new Collection__string('foo', 'bar'));
namespace My\Name\Space;
use My\Generic\Collection;
use My\Domain\Foo;
var_dump(new Collection__Foo(new Foo(), new Foo()));
namespace Some\Other\Name\Space;
use My\Name\Space\Collection__Foo;
function doSomething(Collection__Foo $list)
{
// ...
}
namespace My\Name\Space;
use My\Generic\Collection;
use My\Domain\Foo;
var_dump(new Collection__Collection__Foo(
new Collection__Foo(new Foo(), new Foo()),
new Collection__Foo(new Foo(), new Foo()),
));
namespace My\Generic;
use Stratadox\PhpGenerics\Annotation\Generic;
use Stratadox\PhpGenerics\Generic\T;use function assert;
/** @Generic(count=1) */
class Collection extends \ArrayObject
{
public function __construct(T ...$items)
{
parent::__construct($items);
}
public function offsetSet($index, $value): void
{
assert($value instanceof T);
parent::offsetSet($index, $value);
}
}
namespace My\Generic;
use ArrayObject;
use Stratadox\PhpGenerics\Annotation\Generic;
use Stratadox\PhpGenerics\Annotation\ReturnType;
use Stratadox\PhpGenerics\Generic\T;
/** @Generic(count=1) */
class Collection extends ArrayObject
{
public function __construct(T ...$items)
{
parent::__construct($items);
}
public function offsetSet($index, $value): void
{
assert(is_int($index) || null === $index);
assert($value instanceof T);
parent::offsetSet($index, $value);
}
/** @ReturnType(force="T") */
public function offsetGet($index)
{
return parent::offsetGet($index);
}
}
namespace My\Name\Space;
use ArrayObject;
use My\Generic\Collection;
use Stratadox\PhpGenerics\Annotation\ReturnType;
final class Collection__string extends Collection
{
public function __construct(string ...$items)
{
ArrayObject::__construct($items);
}
public function offsetSet($index, $value): void
{
assert(is_int($index) || null === $index);
assert(is_string($value));
ArrayObject::offsetSet($index, $value);
}
/** @ReturnType(force="T") */
public function offsetGet($index): string
{
return ArrayObject::offsetGet($index);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.