You definitely can write PHP in a functional style. It’s missing some of the niceties in syntax of a functional language though.
Mostly you’re just writing code to minimize the amount that you mutate data.
If you’re writing in a functional style in PHP it can still be useful to use OO concepts like classes as, essentially, tiny namespaces where you can determine the level of privacy of methods. Interfaces and abstracts can also be pretty useful to define custom types.
It's a different language, but check out the ramda library for examples of really awesome functional programming. The documentation is great. Chris Okahravi has a superb video tutorial series on every single function in ramda too. —> youtube.com/playlist?list=PLrhzvIc...
All good. :D I'm no FP expert and welcome corrections. Who knows... maybe my comments will yield some answers and it'll turn out that what I didn't think you could do easily, you can.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You definitely can write PHP in a functional style. It’s missing some of the niceties in syntax of a functional language though.
Mostly you’re just writing code to minimize the amount that you mutate data.
If you’re writing in a functional style in PHP it can still be useful to use OO concepts like classes as, essentially, tiny namespaces where you can determine the level of privacy of methods. Interfaces and abstracts can also be pretty useful to define custom types.
Such as?
These are some nice to have features in functional programming languages.
Function can be passed to other functions and returned as values. In short functions should be treated as values.
closures the inner function can use the variables of outer function regardless of where it is running.
Map, filter, reduce like functions.
pipeline operator.
Unless I'm misunderstanding the terms, I believe PHP has all of these features except a pipeline operator.
It's a different language, but check out the ramda library for examples of really awesome functional programming. The documentation is great. Chris Okahravi has a superb video tutorial series on every single function in ramda too. —> youtube.com/playlist?list=PLrhzvIc...
If you REALLY want to get into it, check out Prof. Frisby's Mostly Adequate Guide to Functional Programming github.com/MostlyAdequate/mostly-a...
+1 for Frisby’s
Pipeline and pattern matching. There’s probably some more syntactic sugar I’m not thinking of right now.
Basically it just results in “uglier” code.
You could probably approximate pipeline with some method chaining if you didn’t want to nest array functions as arguments to other array functions.
You can simulate pipeline type stuff with array reduce and unary functions.
What I really miss is built in currying/partial application, less verbose anonymous function syntax, and auto closures.
Oh, and auto return statements.
When you get used to writing JS and other FP stuff like (x,y)=> x + y and you can do (x) => (y) => x + y, it's really nice.
Pretty sure you can curry in PHP?
Edit: never mind, read your post wrong.
All good. :D I'm no FP expert and welcome corrections. Who knows... maybe my comments will yield some answers and it'll turn out that what I didn't think you could do easily, you can.