Developer654079525's avatar

Type hinting

In production, are you actually using type hinting or not? Specifically for relationship and controller methods. Do you treat it as a nice-to-have or something actually worth having?

1 like
5 replies
vincent15000's avatar

It depends on the case.

Sometimes it's useful when you need to ensure that a variable has a specific type.

1 like
Glukinho's avatar

The more specific and explicit you are the easier your life is. So, use types everywhere you can.

2 likes
JussiMannisto's avatar

It provides type safety, which helps you catch bugs. It's also a hint for your IDE, allowing auto completion. So yes, definitely.

I always use types when possible, because there's no good reason not to. If you want to allow any type, you can use mixed. You still can't type-hint resource (e.g., a file handle), because it's not an actual type in the Zend Engine.

2 likes
simotion's avatar

Yes, I would advice making a habit out of it. It makes your code robust, self documenting and makes your life easier during debugging and coding (autocomplete).

No need for docblocks if you have good attribute names with typehinting and typehinted the return types. Helps making your code easy to read.

2 likes
martinbean's avatar

@developer654079525 I don’t really know what you mean by “in production”? The code I write locally, is the code that then gets deployed to production.

Do I type-hint my code? Yes. I don’t understand why you wouldn’t want a label saying, “This method will return something of type X.” Most text editors and IDEs will then show you an error when you return something other than type X. Again, why wouldn’t you want that early feedback that your code isn’t going to behave as expected?

1 like

Please or to participate in this conversation.