Web agency » Digital news » PHP Best Practice: Type Parameters

PHP Best Practice: Type Parameters

There are the practices that we learn at school, the ones we take on through laziness, the ones we adopt out of habit, whether by dint of seeing it or by systematically copying and pasting. With time and experience, it is clear that we are looking for ever clearer methodologies and syntaxes. Despite the trends that are distinguished around each language, we are often able to determine by reading code, which of our team has such a syntax In C #, purists will get into the habit of starting all their interfaces with an "I" For example.

PHP, a free syntax… too free?

I myself have several habits that allow me to build my code in a regular way. This in order to easily find my way around, to work quickly, but also to facilitate debugging and reading my code for my colleagues.

Type its parameters, for what? And why is this a good practice?

In PHP, the syntax is very free. I mean very free by the fact that the language is not very verbose. Without being a supporter of languages ​​like Java for example, I think that typing its parameters can only help us in our work.

Why waste time writing the data type?
Indeed, but let's immediately compare the pros and cons.

In the cons, without being bad times, I will find it difficult to cite another argument than the one I wrote above.

In the pros, I could state a small list:

  1. It helps me build my code. In my prototype (declaration of my function), I know that such and such an argument must be an array ou int.
  2. This allows me to debug faster. If you declared that you wanted an argument of type User in your method, PHP will expect a User instance. Or try to give an array or any other type other than the one stipulated, will cause PHP to stop immediately. Admittedly, the error risks being quite "hard", but all the rest of your development is not likely to have a failure.
  3. My colleagues like to read my code like a book. Indeed, when we know that the setItem(…) method takes an argument of type Foo, it helps. No ?
    function setItem($item);
    function setItem(Foo $item);

Admittedly, this list contains only 3 points, but our work obliges us to always be on a war footing and to always explore new avenues, I advise you to try and see for yourself.

A suspended issue with the type mixed

Indeed, I must all the same admit a weakness, the type mixed. Horrible guy and to be banned. For those who have coded in C/C++, it's like declaring an instance pointer with a (void *).

I advise you as much as possible not to use this type and to limit its use. An example ?
The method setPoireau(mixed $poireau) takes leeks as parameters but since it is not typed, you can also give it a baby seal… Problematic.

If you wanted to force having leeks, you know what you have to do, and let's admit that you have a twisted mind, and that you would like to be able to give vegetables, I invite you to use an interface (or a class abstract) and define your method like this.

function setLeek(I Vegetable $leek);

I will agree that this example may not be the best there is, but it has the merit of being clear.


The typing is not mandatory, but remains available to anyone who wants to work clearly and cleanly. I strongly recommend it to you.

Perhaps we will have the right, one day, to type the return of a method to help us in our quest for ever better semantics.

★ ★ ★ ★ ★