Skip to content

Code-Of-The-Crescent/php-generics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

75 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP generics written in PHP

Require

  • PHP >= 7.4
  • Composer (PSR-4 Autoload)

Quick start

Install library

composer require mrsuh/php-generics

Add directory for generated files("cache/") to autoload (directory must be placed before the main directory) composer.json

{
    "autoload": { 
        "psr-4": {
            "App\\": ["cache/","src/"]
        }
    }
}

Example

Add generic class src/Box.php

<?php

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;
    }
}

Add usage generic class src/Usage.php

<?php

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
    }
}

Add test with Usage class and composer autoload bin/test.php

<?php

require_once __DIR__ . '/../vendor/autoload.php';

use App\Usage;

$usage = new Usage();
$usage->run();

Generate concrete classes from generics

composer dump-generics -vv

Dump autoload classes

composer dump-autoload

Run bin/test.php script

php bin/test.php

See the tests folder for more examples.

Tests

How to run tests?

php bin/test.php

How to add test?

  • Add directory 00-your-dir-name to ./tests
  • Generate output files and check it
php bin/generate.php tests/000-your-dir-name/input tests/000-your-dir-name/output 'Test\'
  • Test output files
php bin/test.php

About

PHP generics written in PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%