A PHP class usefull to decorate functions


This class represent the beginning of functional programming in PHP.
The decorator patter can easly make your application modular while functions transform it in to an event-driven application.

The goal of this class is to decorate functions, taking advantage of objects to make a queue-like structure and store in it partial results and functions.

Note that with the other my class it can show you a full dynamic decorated system usefull, for example, for a plugin system or to build a modular application.


Usage it's pretty easy: require the main class

 require "Decorate.php";

and then you can use its 2 methods:


 $function = Decorate::onBefore($function, $newFunction);



 $function = Decorate::onAfter($function, $newFunction);


the return: FunctionEmulator

This object is really a function emulator and you can use it as a standard function, but you also call its method!
Expecially you can use this object to decorate him again like a callable function.
Not only,it's a object where inside are stored the original function and the new one. You can use these methods:

Advanced usage

If have you noticed that functions aggregations can be represented and easily manipulated by a LinkedList, then you got it!
I provide two implementations of LikedList:

These classes extends the standard PHP SplDoublyLinkedList so you can add,replace,move etc functions as API say.
Also ,implementing the FunctionEmulable interface they can be called as functions.
An example:

    require '../../Decorate.php';

    $a = new OnAfterDecorativeLinkedList();
    $a->push(function(){ echo 1;});
    $a->push(function(){ echo 2;});
    $a->push(function(){ echo 3;});
    $a(); // print 123

    echo '<br>';

    $a = new OnBeforeDecorativeLinkedList();
    $a->push(function(){ echo 1;});
    $a->push(function(){ echo 2;});
    $a->push(function(){ echo 3;});
    $a();  // print 321