iggyvolz / generics
dev-main
2021-08-18 04:32 UTC
Requires
- iggyvolz/classgen: ^0.1.0
- iggyvolz/simple-attribute-reflection: dev-main
- nette/php-generator: ^3.5
- nikic/php-parser: ^4.12
Requires (Dev)
- nette/tester: ^2.3
This package is auto-updated.
Last update: 2024-10-18 11:31:24 UTC
README
Generics in PHP using classgen
<?php namespace Iggyvolz\Generics\test; use Iggyvolz\Generics\Generic; use Iggyvolz\Generics\T1; #[Generic(T1::class)] class TestGeneric { #[T1] public function foo(#[T1] $var) { return $var; } }
Can be used as:
$x = new TestGeneric«int»();
Which generates:
<?php namespace Iggyvolz\Generics\test; #[\Iggyvolz\Generics\Generic('Iggyvolz\Generics\T1')] class TestGeneric«int» { #[\Iggyvolz\Generics\T1] public function foo(#[\Iggyvolz\Generics\T1] int $var): int { try {'Iggyvolz\\Generics\\T1'::push('int');return $var; } finally {'Iggyvolz\\Generics\\T1'::pop();} } }
Multiple parameters can be passed:
#[Generic(T1::class, T2::class)] class TestGeneric2 { public static function concat(#[T1] $var, #[T2] $var2): string { return $var . $var2; } }
Generates:
class TestGeneric2«int‚string» { public static function concat( #[\Iggyvolz\Generics\T1] int $var, #[\Iggyvolz\Generics\T2] string $var2, ): string { try {'Iggyvolz\\Generics\\T1'::push('int');'Iggyvolz\\Generics\\T2'::push('string');return $var . $var2; } finally {'Iggyvolz\\Generics\\T1'::pop();'Iggyvolz\\Generics\\T2'::pop();} } }
Yes, « and » and ‚ are valid in PHP class names. No that's not a comma (0x2C) but a "single low-9 quotation mark" (x82) which is valid for PHP class names.