Modern PHP is mostly written Object Oriented. Other than the fact that there is inertia towards the Object Oriented in the PHP community, is there anything about the language itself that would make it a poor idea to use functional programming?
-
1You can do functional programming in a limited way in any language that has functions. But to get the full benefits of functional programming, the language has to support functional programming as a first-class programming paradigm.– Robert HarveyCommented Jun 29, 2016 at 16:21
-
@RobertHarvey can you explain or point me to a question/answer that addresses that?– GooseCommented Jun 29, 2016 at 16:28
-
phptherightway.com/pages/Functional-Programming.html– Robert HarveyCommented Jun 29, 2016 at 16:29
-
@RobertHarvey "PHP supports first-class functions", does this mean that PHP gives the full benefits of functional programming?– GooseCommented Jun 29, 2016 at 16:33
-
First-class functions are one of the features that functional programming languages have, but there are others. See en.wikipedia.org/wiki/Functional_programming#Concepts– Robert HarveyCommented Jun 29, 2016 at 16:46
1 Answer
There's nothing missing from the language itself that would prevent you from doing functional programming. The only thing missing from the runtime is tail call optimization, and you can actually do quite a bit of FP before hitting that limitation.
What's really going to hurt you if you try to do FP in PHP is lack of library support for it. You need a fundamentally different collections library, and functional programmers use more semantically precise library functions in many cases where imperative programmers use built-in loops and other language features. Without that library support, you can technically do FP, but you're going to be reinventing the wheel a lot, and likely not doing what other programmers would recognize as idiomatic FP.
-
...but you're going to be reinventing the wheel a lot, and likely not doing what other programmers would recognize as idiomatic FP.
Reinventing the wheel is right, but I'd imagine an FP programmer would recognize it. In my opinion the real issue is that PHP programmers won't recognize it as idiomatic PHP.– DovalCommented Jun 29, 2016 at 17:52