The state of functional programming in PHP

With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. 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

What exactly defines a functional programming language, is perhaps an open question, but one key element is functions as data. As it happens, PHP kind-of-supports this concept; The syntax permits you to use variables as function-names, making the following possible:

 function add($a, $b) {   return $a + $b; } $add = "add"; $add(2, 8); // return 10 

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. It may just be a wrapped up eval, but superficially it works similar, once the function has been declared.

It is also possible to explicitly call a function reference with call_user_func. This is interesting, because it accepts different types of arguments, which makes it possible to call a method on an object. More on this in a moment.

Binding state

Another prerequisite for functional programming, is the ability to bind a variable to a function. This bound function serves essentially the same purpose as objects do in object oriented programming, but is usually more fine grained and more flexible.

In languages, which are traditionally associated with functional programming, functions are usually bound with variables, through something called a closure. This is a side effect of the scoping rules of those languages.

Since PHP doesn’t have lexical scope, we can’t use closures, but we can use currying to achieve the same goal. In Wikipedia’s words, currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument. If that sounds abstract, assume the following:

 $add2 = curry($add, 2); // returns a new function reference, to add(), bound with 2 as the first argument $add2(8); // return 10 

Before you try that out, hold your horses — it won’t work, because curry isn’t a PHP function. As it turns out though, it’s possible to create it. Sort of.

Implementing curry

Without going into great details, there are two ways, currying can be implemented in PHP.

For a more detailed explanation, have a look at Partial function application in PHP.

Weighing the options

Looking at the syntax, variable-variables have a much more functional “feel” to them, than a command object. The callback pseudotype does allow one to reference an object + method-name, but even though this is supported by PHP’s internal functions, it get’s bulky in user land code. If a command object feels like functional programming with OOP syntax, then call_user_func feels like functional programming with procedural syntax.

When it comes to performance, the run-time evaluated approach has some serious shortcomings. Currying must be done by creating new static code each time and this can’t be reclaimed until the end of the process. Furthermore, the only way to bind state, using this pattern, is to rely on a global container of some sort, generating a new, unique symbol each time. Since it’s impossible to know, when the callback isn’t referred anymore, it follows, that it’s impossible to safely remove the variable from the global container. Thus, any state bound this way, will be impossible to reclaim for PHP’s memory management system, furthering the risk of the script running out of memory.

In conclusion

So, if the offset for this post was to assert the current options for functional programming in PHP, the only practical solution is currying, using command objects. The awkwardness can be somewhat smoothed out, with a library of utility classes. phunctional is an attempt at this and we might also see some of these ideas emerge into more general purpose frameworks.

Another idea, could be a language level addition to PHP. What we need, is a way to make all callbacks callable with the variable-variable syntax. I’m thinking this could be supported with a magic-method. If an object was to be used as a function, and it implemented said magic method, the method would be called. From a design perspective, it would fit fine with the other magic methods, such as __call and friends.

Declaring functions

Another limitation, which could be addressed at the language level, is the matter of declaring functions in the first place. Currently, functions must be declared in the global scope or through the use of the hideous create_function. There have been some stabs at improving this, on the php-internals team, so maybe we should be as lucky as to see this in PHP 6?

Replay

Category: programming Time: 2007-12-15 Views: 1
Tags:

Related post

  • Functional Programming and PHP 2013-02-25

    Many programmers like to talk about functional programming, but if you ask them if they've ever done it, most of their replies will be "No". The reason is quite simple: we are taught to think in an imperative manner when we first start learning

  • Do non-pure interpreters still make the guarantees of functional programming? 2012-04-18

    I am assuming the implementations/compilers/generated C code (referred to hereinafter as generic, 'interpreter') for most functional programming languages are written in non-pure functional languages. If this is the case, the underlying interpreter f

  • How are the concepts of functional programming relevant to imperative programming? 2016-02-05

    What are the concepts from functional programming that might be applied to imperative programming? --------------Solutions------------- Immutable values: Immutable values can be used in place of some mutable variables, removing the possibility of an

  • Is programming in the UNIX philosophy the same as Functional programming? 2011-03-22

    The UNIX Programming Environment (the classic text) states that the UNIX approach to programming is to build small, well-defined tools that can be combined to solve more complex problems. In learning C and the Bash shell, I've found this to be a powe

  • How to make the transition to functional programming? 2012-04-24

    Lately, I have been very intrigued with F# which I have been working a bit with. Coming mostly from Java and C#, I like how concise and easily understandable it is. However, I believe that my background with these imperative languages disturb my way

  • What exactly is procedural programming? How exactly is it different from OOP? Is it the same as functional programming? 2014-02-27

    I am programming in Java in a very object-oriented (OO) style. OOP comes very intuitively to me, but I have very little knowledge about other kinds of programming. What exactly is procedural programming? How exactly is it different from OOP? Is it th

  • Functional Programming in PHP 2016-06-29

    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? ------

  • What does "polyadic" mean in the context of functional programming and type systems? 2012-12-09

    And how does it (or does not) correspond to "polymorphic"? Occasionally I see this notion like in: Implement and represent polyadic operations. I checked Wiktionary but it only gives a general meaning, not specific to functional programming. ---

  • What is the state of XMEGA programming from Linux? 2010-08-11

    What are the hardware and software options for programming an XMEGA from Linux? --------------Solutions------------- I find that command-line avr-gcc and avrdude work great on Mac OS X for programming xmega; Should work equally well under linux. Use

  • Why the current enthusiasm for Functional Programming? 2010-11-19

    I've been hearing a lot of enthusiasm about functional programming languages lately, with regards to Scala, Clojure, and F#. I've recently started studying Haskell, to learn the FP paradigm. I love it, it's really fun, and fits my math background. Bu

  • Functional Programming on the rise? 2011-04-26

    I have noticed lately that functional programming languages are gaining popularity. I recently noticed how the Tiobe Index shows an increase in their popularity in comparison to the last year although most of them do not even reach the top 50 most po

  • What statements and approaches should I avoid when learning functional programming? 2012-08-12

    I have 6 years of programming experience, mostly following the object oriented paradigm, and I'm interested in learning functional programming. My main goal is to become a functional paradigm programmer and not a programmer who is using a functional

  • Pure functional programming and game state 2014-04-25

    Is there a common technique to handle state (in general) in a functional programming language? There are solutions in every (functional) programming language to handle global state, but I want to avoid this as far as I could. All state in a pure func

  • Functional programming, compared to the process of a computer 2015-09-18

    This question already has an answer here: Isn't functional paradigm too divergent with underlying hardware to be generally efficient? 4 answers In functional programming, it is considered bad practice (at least from my observations) to use state chan

  • Are there any academic papers on the merits of imperative over functional programming? 2011-09-11

    There are lots of academic papers on the merits of functional programming floating around. There's also a lot of debate that I see (on the internet) going on about the relative merits of imperative vs. functional programming. I'm interested to know i

  • Could the "reduce" function be parallelized in Functional Programming? 2016-02-06

    In Functional Programming, one benefit of the map function is that it could be implemented to be executed in parallel. So on a 4 cores hardware, this code and a parallel implementation of map would allow the 4 values to be processed at the same time.

  • Is Javascript a Functional Programming Language 2011-12-30

    Is Javascript a functional language? I know it has objects & you can do OOP with it also, but is it also a functional language, can it be used in that way? You know how OOP became/seems like the next evolution in programming, does that mean that 'Fun

  • Functional programming compared to OOP with classes 2012-12-19

    I have been interested in some of the concepts of functional programming lately. I have used OOP for some time now. I can see how I would build a fairly complex app in OOP. Each object would know how to do things that object does. Or anything it's pa

  • Help me to understand 'stateless' software in functional programming 2014-10-05

    I'm an experienced software engineer, and have experience in a range of languages from PHP, Ruby, C#, Java - the last few years I've primarily worked in Java. I'm now learning Scala, and it's my first real foray into functional programming. I underst

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 4.611 (s). 13 q(s)