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?

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

1 Answer 1

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.

1
  • ...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
    Commented Jun 29, 2016 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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