1. Go to this page and download the library: Download mrsuh/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/ */
mrsuh / php-generics example snippets
namespace App;
class Box<T> {
private ?T $data = null;
public function set(T $data): void {
$this->data = $data;
}
public function get(): ?T {
return $this->data;
}
}
namespace App;
class Usage {
public function run(): void
{
$stringBox = new Box<string>();
$stringBox->set('cat');
var_dump($stringBox->get()); // string "cat"
$intBox = new Box<int>();
$intBox->set(1);
var_dump($intBox->get()); // integer 1
}
}
pp\Usage;
$usage = new Usage();
$usage->run();
namespace App;
class Box<T> {
private ?T $data = null;
public function set(T $data): void
{
$this->data = $data;
}
public function get(): ?T
{
return $this->data;
}
}
namespace App;
class BoxForInt {
private ?int $data = null;
public function set(int $data) : void
{
$this->data = $data;
}
public function get() : ?int
{
return $this->data;
}
}
namespace App;
use App\Entity\Cat;
use App\Entity\Bird;
use App\Entity\Dog;
class Test extends GenericClass<Cat> implements GenericInterface<Bird> { // <-- extends/implements
use GenericTrait<Dog>; // <-- trait use
private GenericClass<int>|GenericClass<Dog> $var; // <-- property type
public function test(GenericInterface<int>|GenericInterface<Dog> $var): GenericClass<string>|GenericClass<Bird> { // <-- method argument/return type
var_dump($var instanceof GenericInterface<int>); // <-- instanceof
var_dump(GenericClass<int>::class); // <-- class constants
var_dump(GenericClass<array>::CONSTANT); // <-- class constants
return new GenericClass<float>(); // <-- new
}
}
namespace App;
class Test<T,V> extends GenericClass<T> implements GenericInterface<V> { // <-- extends/implements
use GenericTrait<T>; // <-- trait use
use T; // <-- trait use
private T|GenericClass<V> $var; // <-- property type
public function test(T|GenericInterface<V> $var): T|GenericClass<V> { // <-- method argument/return type
var_dump($var instanceof GenericInterface<V>); // <-- instanceof
var_dump($var instanceof T); // <-- instanceof
var_dump(GenericClass<T>::class); // <-- class constants
var_dump(T::class); // <-- class constants
var_dump(GenericClass<T>::CONSTANT); // <-- class constants
var_dump(T::CONSTANT); // <-- class constants
$obj1 = new T(); // <-- new
$obj2 = new GenericClass<V>(); // <-- new
return $obj2;
}
}
namespace App;
class Box<T> {
private ?T $data = null;
public function set(T $data): void
{
$this->data = $data;
}
public function get(): ?T
{
return $this->data;
}
}
namespace App;
class Box {
private $data = null;
public function set($data) : void
{
$this->data = $data;
}
public function get()
{
return $this->data;
}
}
namespace App;
use App\Entity\Cat;
use App\Entity\Bird;
use App\Entity\Dog;
class Test extends GenericClass<Cat> implements GenericInterface<Bird> { // <-- extends/implements
use GenericTrait<Dog>; // <-- trait use
private GenericClass<int>|GenericClass<Dog> $var; // <-- property type
public function test(GenericInterface<int>|GenericInterface<Dog> $var): GenericClass<string>|GenericClass<Bird> { // <-- method argument/return type
var_dump($var instanceof GenericInterface<int>); // <-- instanceof
var_dump(GenericClass<int>::class); // <-- class constants
var_dump(GenericClass<array>::CONSTANT); // <-- class constants
return new GenericClass<float>(); // <-- new
}
}
namespace App;
class Test<T,V> extends GenericClass<T> implements GenericInterface<V> { // <-- extends/implements
use GenericTrait<T>; // <-- trait use
private GenericClass<V> $var; // <-- property type
public function test(T|GenericInterface<V> $var): T|GenericClass<V> { // <-- method argument/return type
var_dump($var instanceof GenericInterface<V>); // <-- instanceof
var_dump(GenericClass<T>::class); // <-- class constants
var_dump(GenericClass<T>::CONSTANT); // <-- class constants
return new GenericClass<V>(); // <-- new
}
}
namespace App;
class Generic<in T: Iface = int, out V: Iface = string> {
public function test(T $var): V {
}
}
const FOO = 'FOO';
const BAR = 'BAR';
var_dump(new \DateTime<FOO,BAR>('now')); // is it generic?
var_dump( (new \DateTime < FOO) , ( BAR > 'now') ); // no, it is not
namespace App;
class Usage {
public function run() {
$map = new Map<Key<int>, Value<string>>();//not supported
}
}
namespace App;
class GenericClass<T, varType, myCoolLongParaterName> {
private T $var1;
private varType $var2;
private myCoolLongParaterName $var3;
}
namespace App;
class Map<keyType, valueType> {
private array $map;
public function set(keyType $key, valueType $value): void {
$this->map[$key] = $value;
}
public function get(keyType $key): ?valueType {
return $this->map[$key] ?? null;
}
}
namespace App;
class Map<keyType = string, valueType = int> {
private array $map = [];
public function set(keyType $key, valueType $value): void {
$this->map[$key] = $value;
}
public function get(keyType $key): ?valueType {
return $this->map[$key] ?? null;
}
}
namespace App;
class Usage {
public function run() {
$map = new Map<>();//be sure to add "<>"
$map->set('key', 1);
var_dump($map->get('key'));
}
}