A functional programming PHP framework?
I cannot find a "Functional Programming" PHP framework anywhere. I wonder why that is. And what would a functional programming PHP framework even look like? What would Laravel look like if it was made using FP?
@Sinnbeck this one looks 10 years old. Functional Programming wasn't even a thing back then, it has become popular only recently.
And I'm not sure if Functional Programming and Procedural Programming is the same thing.
@martinszeltins nope not quite the same but it was the closest I could find. And yeah don't think anyone makes functional php frameworks. But imagine you only had the functions in laravel. No models. So want to make a query? You could perhaps call alot of seperate functions one by one passing variables. And you would get an array of arrays back instead of classes. And that's just the query builder
@Sinnbeck That sounds absolutely horrible. So I don't understand the recent buzz around Functional Programming. People saying that OOP is bad and FP is the new deal. But from what you described it sounds like 💩
@martinszeltins I think it's been going on since I started coding. But every tool for the right job. My example was for how laravel works as that's what you asked. If I wanted to write a python script I would most likely wrote it completely using functional programming (I have in the past)
@Sinnbeck I see. Why would you write it using FP instead of OOP?
Functional Programming wasn't even a thing back then
@martinszeltins Give over. Functional programming has been a thing for decades as it holds its origins in traditional mathematics. You won’t find frameworks for functional programming because there’s nothing a framework could offer. You define first-class functions, the inputs they take and their outputs. There’s nothing a framework could add here or do any heavy lifting of.
Given you don’t really seem to understand functional programming, I don’t really understand why you’re trying to find a framework for it?
@martinszeltins my last big project was a photo booth. So I wrote a function for each function it could do and called them in a loop in main. No reason for classes for anything. Each function did its just and they could call each other if needed
Also didn't notice the functional programming is new thing. It's older than OOP :)
@martinszeltins, ah...
"Functional Programming wasn't even a thing back then, it has become popular only recently."
That's decidedly false and a mis-perception. FP has been around and available for decades. It occasionally does enjoy a renaissance or "re-discovery", as in this industry everything old is new again, at some points along the way.
"And I'm not sure if Functional Programming and Procedural Programming is the same thing."
Certainly not the same thing. Early days of PHP and especially prior to introduction of OOP in PHP (but also after that) saw an awful lot of procedural (spaghetti) code in php projects... not FP, however.
There are probably dozens of efforts toward FP-in-PHP projects out in the wild, but maybe none or not many of them have matured or had much staying power or garnered community.
See also:
Functional Programming wasn't even a thing back then
Lisp
- First appeared: 1958; 64 years ago
- https://en.wikipedia.org/wiki/Lisp_(programming_language)
Haskell (Paradigm: Purely functional)
- First appeared: 1990; 32 years ago
- https://en.wikipedia.org/wiki/Haskell
Scala
- First appeared: 20 January 2004; 18 years ago
- https://en.wikipedia.org/wiki/Scala_(programming_language)
F#
- First appeared: 2005; 17 years ago, version 1.0
- https://en.wikipedia.org/wiki/F_Sharp_(programming_language)
Clojure
- First appeared: 2007; 15 years ago
- https://en.wikipedia.org/wiki/Clojure
You won’t find frameworks for functionality programming because there’s nothing a framework could offer
I agree with @martinbean
But in case you want a library to help you with functional primitives:
@rodrigo.pedra lol you Google better than me. 😨 Or maybe I searched on packagist
A lot of practice in GDD (Google-Driven Development) =)
@rodrigo.pedra haha yeah. I think I used packagist and got bad hits. But if I search by the names I can find them. Never would have guessed that there would be that many!
I insisted on searching because I remembered seeing one of them some time ago. Then I stumbled upon an article: "7 top PHP functional libraries", and that was gold =)
One thing that might have helped, I actually use DuckDuckGo for most of my searches.
Google these days only when I want to see opening hours or directions for local business.
Lately, at least for me, first two pages of Google are just sponsored ads. Maybe here in Brazil they like tossing too much money to Google for the same keywords.
Oh, and the one I remembered was the last one I found !
As the old saying: "what you search is always in the last place you look for" (free translation).
(which is actually kind of obvious, as no one would keep searching for something they already found)
@rodrigo.pedra exactly 😊 yeah duck duck go is brilliant. Hate the page of adds
In database management you shouldn't really need this sort of programming.
In a way the framework portion has some functional programming techniques.
I look at functional programming kind of like in freecad python where I code a definition to return something.
An example would be a reducing round elbow, I use a definition to return elliptical polygons. The def gets passed in parameters.
Basically here if you stick with laravel conventions and follow good MVC you are good to go.
Also remember OOP and patterns are for humans the CPU at runtime could care less what kind of code it is, ugly, pretty, or procedural it is going to run it.
I think people are confusing procedural programming with functional programming. They are not the same thing. The opposite of OOP is not FP. Just because your program is written using functions does not make it FP yet. Yes, in a sense FP has been around for a long time but only become popular recently.
Functional Programming is about writing pure functions.
- Always return a single value from a function.
- This value that you return should only be based on the input you give to the function. You cannot use anything outside of the function.
- You cannot mutate any existing value.
I think people are confusing procedural programming with functional programming. ... The opposite of OOP is not FP.
Can you point which answers here, if any, hints these?
Haskell for example is a pure functional language since its inception. Lisp allows other types of programming, but is regarded of one of the first functional languages. Clojure is a Lisp variant, but more constrained towards functional programming. Scala and F# also allow other types of programming, but are also widely used within functional programming domain.
Functional Programming is about writing pure functions.
No actually. Where did you see this definition?
Otherwise all functional programs would need to run in a "wrapper" environment/runtime, as output needs to be written to files, database, etc. Or even writing to a screen, as it is a side-effect.
I agree using pure functions and immutable values are like the spine of functional programming application. At least mainstream tutorial and material are towards that.
But I assume you already have seen that real-life code used on production environments looks very different than the code from books and tutorials, right? This is true for OOP, procedural, declarative, and also for functional programming.
In functional programming, impure functions are tried to be kept at a program boundary, close to outside world interaction. And mutation are tried to be confined in persistent data-structures (such as atoms in clojure), or "messaged" out with something like actors.
But using only pure functions can help you adding functional programming into other paradigms, such when using collection pipelines. But wouldn't allow functional program, in for example Haskell, to be useful.
Always return a single value from a function.
Well this is true to must programming languages, isn't it?
If you mean they should return a single scalar value, I guess you never saw any sorting algorithms implemented with functional programming (which return arrays), or any custom Type -- a strong type system is actually a foundational, not mandatory, concept in FP -- or the Maybe type, which are composable types.
If you mean having a single return point, well even functions considered pure are not required to not work that way.
Immutable types are a goal, but also not mandatory in functional programming. At least to actually useful applications, in the sense of real-life applications, of course many theoretical programs are useful on advancing the field.
But pure theoretical applications wouldn't help sorting a live call-center overflow call queue, calculating a person's credit-score, deciding on-demand and live if an operation is a fraud (which may collect a lot of external input along the processing upon "folding"/branching, making the whole pipeline impure) and other applications where functional programming have been gaining relevance since the 90's.
This is a great talk about real-life Functional Programming:
https://www.youtube.com/watch?v=0if71HOyVjY
(when searching for it -- I've watched it back then -- I found this playlist it is embed in with some other great talks on the subject -- I've seen some, but not all: https://www.youtube.com/watch?v=0if71HOyVjY&list=RDLV0if71HOyVjY )
This value that you return should only be based on the input you give to the function. You cannot use anything outside of the function
True for pure functions. Just for them. I won´t repeat the arguments above.
You cannot mutate any existing value
In theoretical examples and tutorials you might not (even there is fine).
In real-life applications you can't avoid side-effects, state or mutation.
Maybe you are mixing functional programming with lambda calculus, which was foundational to the development of functional programming.
in a sense FP has been around for a long time but only become popular recently.
Maybe you are more aware about FP recently? Or in your field of programming (maybe web programming, or application programming) influential people, started talking about it recently?
Talk to any one who work in telecom -- actually on writing software for telecom -- and ask them when functional programming became mainstream.
Read about Erlang adoption in telecom, and how GPRS, 3G technologies were made possible (in terms of efficiency) with functional programming.
FP has been the spine of messaging systems, from memory I think RabbitMQ used Erlang. WhatsApp backend infrastructure is famously written in Erlang. Data ETL benefits a lot from FP.
Maybe FP became more widely adopted out of the infra-structure and into application/web programming recently, on that I can kind of agree.
But FP is around, and used in a lot of real-life fields, for way more then a decade.
PS: I am claiming to be a FP expert. Nor I am not claiming using it extensively on my real-life projects -- I do use it here and there, where it makes sense, although I did use it more on previous projects and day-job (not always by my choice) more than a decade ago.
If a Functional Programming framework is for you, you could always write your own. I have a custom php framework I have had long before there was a laravel, I just keep it up to date with php changes.
But how to you plan on maintaining State.
@jlrdw I don't think it is possible to create a PHP framework using Functional Programming because it is not meant for that purpose. You cannot maintain State because it does not exist in FP.
@martinszeltins I know, but you did ask about one. You can however have parts of a php framework FP.
But really I would never bother, if such as that is needed I'd use python.
Edit:
Meaning python for mathematical functions. I wouldn't even consider FP for database management.
In the 1980's I would use Basic for math, Dbase 3 for DB management.
@martinszeltins It’s a bit odd to ask about frameworks for functional programming, a day later surmised that it’s impossible to create a framework for functional programming due to the very nature of functional programming, and then award yourself the “best answer”.
@martinbean I know what it looks like 😀 But that is the conclusion that I came to during our discussion which I believe should have been the correct answer all along. I didn't accept my answer as "best answer" to get an award but so that other people who have the same question could find the answer faster. I hope that makes sense.
@martinszeltins well I knew that already that's why I asked you how would you maintain state.
And it is possible in PHP but you would have to pass what would be in state to every function.
It would be a combination of a migraine headache and a worst nightmare.
your own "best answer" is basically @martinbean 's early answer, but written in a way you knew and agreed with his answer beforehand.
It totally contradicts your opening message where you asked about something that you looked like you didn't know about -- and then in your "best answer" you look like knowing it by heart and experience.
If the idea is future-proofing for people with similar question, I guess seeing the question and answer by the same person, showing such opposite edges of knowledge in the subject, would confuse more than help.
But that is my own opinion, keep it if you wish.
I'm learning laravel11 now and stumbled upon this conversation, I think I created a php framework closed to functional programming. It is not your traditional MVC framework, please look at it and let me know if this framework is a good idea. github.com/vgalvoso/phpeasy
Please or to participate in this conversation.