7
I know PHP is a weakly typed language. But there is some alternative way to "type" a array
in PHP?
Example, I can do this below to force receive a specific type of data in a function.
function exemplo(Classe $valor){
// meu código aqui ...
}
However, I would like to know how to do something similar as in Java (see below) with lists.
List<String> array = new ArrayList<>;
There is a way to do this in PHP?
You want to ensure that within the array there are only values of a specific type?
– Cahe
Yes. By default PHP treats them as Mixed. Queri to set an X type. @Cahe
– Luitame
This is just to improve the IDE’s auto-complete or have something special in your code that you need.
– rray
Actually it’s just mania why I use it in java.
– Luitame
I wouldn’t say it’s all mania, it makes sense but language won’t allow it.
– Maniero
@bigown in his opinion this is a weak point of the language or simply a characteristic inherent to PHP that it is up to each one their respective choice of language for their project?
– Luitame
It is well opinion. I think it is a weak point and a strong point :) Nothing is all good and all bad. In programming we always talk about tradeoffs. We always trade one thing for another. We make choices of which defect we want to live with since nothing is perfect. For large code bases manipulated by large teams in systems that are made to last having no types can make it very difficult. But it also simplifies codes in several situations. It’s getting long, it would even give a good question (asked the right way). If nobody posts anything in the next few days, I do.
– Maniero
Thank you again and I’m looking forward to watching the conversation unfold. @bigown
– Luitame
Discussion may not, only objective answers (or by the least subjective good) :)
– Maniero
It sounded tin, but I expressed it wrong. I didn’t mean DISCUSSION in the aggressive sense. In fact, I talked about debating the issue and each presenting his or her point of view.
– Luitame
And I just joked, I had understood :) But even debate is not allowed nests website.
– Maniero