百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

PhpGenerics

> 编程语言
开源

这里有龙

553 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

这里有龙

Muahahahahahahaha

I'll let the example speak for itself:

Example:

Test/Item.php

php
namespace test;

class Item {
    
    protected $item;
    
    public function __construct(T $item = null)
    {
        $this->item = $item;
    }

    public function getItem()
    {
        return $item;
    }

    public function setItem(T $item)
    {
        $this->item = $item;
    }
}

Test/Test.php

php
namespace Test;

class Test {
    public function runTest()
    {
        $item = new Item;
        var_dump($item instanceof Item); // true
        $item->setItem(new StdClass); // works fine
        // $item->setItem([]); // E_RECOVERABLE_ERROR
    }
}

test.php

php
require "vendor/autoload.php";

$test = new Test\Test;
$test->runTest();

HOW???

Black magic and voodoo.

Where can I use generics?

Right now, only class definitions can define generics, and any parameter or return type declaration can use them.

It also supports parameter-expansion:

php
class Foo {
    public function bar(): Foo {}
}

As far as the rest, I don't know.

Seriously, How???

Like I said, black magic. If you want to know, you're going to regret it.

How do I install?

Since this is black voodoo evilness, I'm not adding it to packagist. Simply add a composer repository pointing here, and composer install. Then just use generics in your code and be happy.

HOW ARE YOU DOING THIS?

You really don't want to know...

Gotchas

Right now, generic types are not resolved according to use rules. So

php
    new Item

Always points to \StdClass. It will not respect use or the present namespace. This is a TODO.

FOR THE LOVE OF GOD, HOW???

Fine. Your loss.

I hijack the composer autoloader, and substitute my own. I then pre-process all autoloaded files, transpiling them to eliminate generics from declarations. I also compile usages from generic syntax to namespace syntax (compiling the types as we go along).

So:

php
new Item

Becomes

php
new Item\①StdClass①

Then, the autoloader recognizes attempts to load these classes and will generate the templated code... The above 2 blocks of code will be compiled to:

php
class test
{
    public function runTests()
    {
        $item = new \test\Item\①StdClass①(new \StdClass());
        $itemList = new \test\ItemList\①StdClass①();
        $itemList->addItem($item);
    }
}

And:

php
namespace test\Item;

class ①StdClass① extends \test\Item
{
    protected $item;

    public function __construct(\StdClass $item)
    {
        $this->item = $item;
    }

    public function getItem()
    {
        return $item;
    }
    
    public function setItem(\StdClass $item)
    {
        $this->item = $item;
    }
}

TL;DR

TL;DR: don't use this

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言