This page is deprecated!

Click here instead

The old page is below.


Functional PHP Extensions SourceForge Logo


A set of higher-order functions and other primatives written in PHP4 which let you write php code in a functional style, similar to the way you might in Haskell, Scheme, or ML. There are also stubby object-to-functional bindings.
  • What
  • Why
  • Documentation
  • Downloads
  • FAQ
  • Documentation:

    It's literate (or at least heavily commented) source code. Read it, and you will learn a lot about PHP and how the library works.

    Alternatively, grep for the word "function". I've tried to keep it to where only the lambda function actually includes the word "function" in the body of the function.

    Ok, so you're impatient. Here are a few highlights:

    • Important note: None of these functions work "in-place" on any data. Always they take input parameters and return results. If you're used to functional programming, then this is intuitive and what you probably expect. On the other hand, if you grew up with the imperative/procedural instruction method, then this may suprise you in connection with some of the recent PHP documentation I recall..
    • lam(, ) - The (proper) lambda function. Unlike php4's Create_Function(), this takes an expression, not a procedure.
    • map(, ) - The returned hash has the original keys in the original order, but the values have been transformed by the function of one argument. This does NOT work "in-place" on an array, but rather creates a new one. All the functions create new data instead of changing existing variables.
    • reduce(, , ="") - Performs an iterative reduction on a list by calling (, ) so the result of your function is the accumulator on the next go-around. You get back the final value of the accumulator.
    • reduce_hash(, , ="") - This version is like the one above, but calls (, , ).
    • AND MANY MORE - In fact, the above is technically more than enough to express any computation. However, this is about making your life easier, not harder. Read the source. You'll find a lot worthwhile in it.


    Objects?

    Ok, so you call methods on objects to get results, right? Almost the same as a function except that:
    • There's an explicit context that you have to name in order to run the code
    • There's the potential for virtual functions
    • Creating a context (object) is a defined process
    • PHP has certain syntactic limitations about how you can work with objects. (e.g. you can't invoke a method on the result of a function.)
    • PHP4 has references. PHP3 does not. This is a fairly big deal.
    • PHP doesn't have destructors, but that doesn't affect us.

    So anyway, the whole point here becomes one of determining which types of interactions are both common with objects and representative of standard fundamental intentions.

    What I've observed about functional programming is the idea that you have collections of stuff that basically similar composite operations happen to. That is, it's largely focused on lists (and in PHP's case, hashes). By this line of reasoning, it seems that the main thrust of an integration would be a way to perform list (or hash) based operations with or on objects. Such operations would, of course, be composable functions.

    Right now, we have a map_method function, which encapsulates a loop around sending a list of things into a unary method. This basically allows objects to operate as closures.

    There are some more common patterns of interaction with objects, but right this minute I haven't taken the time to enumerate them. Anyone with extensive experienece is welcome to make suggestions or send me code.