This is about generic programming, key in statically typed languages. That’s why it has in Hack and not in PHP.
There the T
works as a super variable and will have a value replaced later - this value can only be a data type. When you are instantiating this class you will have to inform about what kind of data the instance will work, after all in statically typed language all data needs a fixed type. This class can box data of any kind. Imagine how crazy it would be to create a new class for each type you need.
In a dynamic language it is simpler because it creates a indirect and treats the data in a generic way and solves at runtime, whether it works or not. In static language needs to solve at compile time giving more security and performance.
It is a way to instruct the compiler to use this class in a special implementation at each instance. Then the compiler will generate on its own a new version of the class for each type effectively instantiated in its application. All the places where there’s a T
will receive the type to be instantiated.
Instantiate yourself with a int
(ex.: new Box<int>
) the generated inner class (you don’t need to know about it) will be something like this:
class BoxInt {
protected int $data;
public function __construct(int $data) {
$this->data = $data;
}
public function getData(): int {
return $this->data;
}
}
I put in the Github for future reference.
You can use whatever identifier you want there, because you can have several in classes that depend on more than one type. But it needs to have coherence of its use in every class. The error may be because spoke change in other places where still had the T
, but do not know, this was not posted, I can only reply speculating.
Do you want to know about the
Box
or the<T>
in general?– Maniero
The
<T>
. TheBox
I already know it’s a class :– Wallace Maxters
Did the answer solve what you were looking to know? Do you think you can accept it now? If not, you need something else to be improved?
– Maniero