Generics in PHP

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

As a C++ guy <T> is the convention way before there was such a thing as PHP. When you get into two or more parameters, would want to describe their usage, just like you would with parameters, as they are parameters. So if the generic can take anything (say a generic collection), then T is fine, as it specifies a completely generic type. If you have something that really only makes sense for a scalar for example, then something like TScalar or TNumeric (that would support math functions on the type) would make more sense. But generic type names should start with a T, as has been the convention in C++ for decades. T says it is a placeholder for a type.

brucekwells
Автор

I like the `T` approach, you get used to it overtime, and that is how Typescript and C# do it

SantiagoArizti
Автор

The reason T is my preferred way when dealing with generics is because it says nothing about the type you're dealing with, as is usually the case when using generics.
However, even when using a bounding box I feel like T is fine. It's not the job of "T" to describe to you what you're dealing with as most likely you'll only be seeing it once at the docblock level and never again in your code.

What's much more useful for code readability is just simply using proper and clear names for your generic values. In another comment I've seen "TScalar" and "TNumber" but why?
When taking a Java (I'm most comfortable with generics in Java) method declaration using those names it provides no benefit over a clear name.

public <TScalar> void doScalarOperation(TScalar scalar) { }
public <T extends Scalar> void doScalarOperation(T scalar) { }

In my opinion the variable name does more than enough to describe to me what I'm working with and using TScalar just reintroduces the infamous verbosity of Java into PHP.

Demozo_
Автор

I prefer using the T prefix (like TValue).

MrSebio
Автор

I’d most likely go for readability over convention, in an attempt to reduce cognitive load where ever possible.

boxxroom
Автор

I just literally treat it as if it were a class.
So in the example, I would just leave it as `InstanceOf`... That said, I realize that doesn't fully work for the `Attribute` one, in which case, I'll prefix with a T, but that is probably because I come from C# land, where we prefix interfaces with I and generics with T.

MarisaClardy
Автор

what do you think about hack php from facebook? it has generics and other cool "typed" stuff, why do you think it's not mainstream?

tdias
Автор

I use the "T" prefix, and an understandable name, trying to use same names across the whole context.

antonkarpov
Автор

I use Camel_Snake_Case, prefixed with GEN and followed by pi, because. Evil. GEN_Instance_Of_π

JeffStoner
Автор

"proper support for generics using docblocks"

I think your definition of "proper" is way weaker than my definition of proper.

Another jewel: "change the color".

Lol, you messed things up by a lot.

FlaviusAspra