
This could be variable-variables, pointing to function names or callback pseudotype, invoked by call_user_func. The first option is some sort of run-time evaluated code.Without going into great details, there are two ways, currying can be implemented in PHP. As it turns out though, it’s possible to create it. $add2 = curry($add, 2) // returns a new function reference, to add(), bound with 2 as the first argumentīefore you try that out, hold your horses - it won’t work, because curry isn’t a PHP function. If that sounds abstract, assume the following: In Wikipedia’s words, currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument. Since PHP doesn’t have lexical scope, we can’t use closures, but we can use currying to achieve the same goal. This is a side effect of the scoping rules of those languages. In languages, which are traditionally associated with functional programming, functions are usually bound with variables, through something called a closure. This bound function serves essentially the same purpose as objects do in object oriented programming, but is usually more fine grained and more flexible. More on this in a moment.Īnother prerequisite for functional programming, is the ability to bind a variable to a function. This is interesting, because it accepts different types of arguments, which makes it possible to call a method on an object. It is also possible to explicitly call a function reference with call_user_func. It may just be a wrapped up eval, but superficially it works similar, once the function has been declared. Unlike languages with first class functions support, the variable $add isn’t a special type - It’s merely a string, which is evaluated in the context. As it happens, PHP kind-of-supports this concept The syntax permits you to use variables as function-names, making the following possible:

What exactly defines a functional programming language, is perhaps an open question, but one key element is functions as data. Even Java seems to be getting closures in the next version, so does this leave PHP lacking behind or is there an unrealised potential hidden within? Dynamic dispatch

With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream.
