2

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?

  • 1
    You 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 Harvey Jun 29 '16 at 16:21
  • @RobertHarvey can you explain or point me to a question/answer that addresses that? – Goose Jun 29 '16 at 16:28
  • @RobertHarvey "PHP supports first-class functions", does this mean that PHP gives the full benefits of functional programming? – Goose Jun 29 '16 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 Harvey Jun 29 '16 at 16:46
3

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. – Doval Jun 29 '16 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.