PHP Classes

Decorate: Alter functions running code before and after

Recommend this page to a friend!
  Info   View files View files (15)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (4 days ago) RSS 2.0 feedStarStarStarStar 72%Total: 375 This week: 2All time: 6,749 This week: 204Up
Version License PHP version Categories
decorate 10BSD License5.4PHP 5, Design Patterns
Description 

Author

This class can alter functions adding code before and after they run.

It implements the decorator design pattern by taking a function and creating a new one that executes another given function before or after the original function is run.

The class returns the newly created function as a reference to the callable function.

Innovation Award
PHP Programming Innovation award nominee
February 2014
Number 5
Sometimes you need to alter the behavior of functions by doing something before or after the original function code. That is the purpose of the decorator design pattern.

This class implements the decorator pattern by calling custom code before or after the original function code.

Manuel Lemos
Picture of Nicola Covolo
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

 

Details

Decorate ======== A PHP class usefull to decorate functions ## Introduction 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 https://github.com/jstar88/Editable it can show you a full dynamic decorated system usefull, for example, for a plugin system or to build a modular application. ## Usage Usage it's pretty easy: require the main class ```php require "Decorate.php"; ``` and then you can use its 2 methods: ### onBefore ```php $function = Decorate::onBefore($function, $newFunction); ``` Where * ```$function``` is the callable function to intercept * ```$newFunction``` is the callable function that will be called before the other one.Note that the arguments of this function are the same of ```$function```, in this way you have all the things usefull to build your interceptor-logic * the method return a ```FunctionEmulator``` class ### onAfter ```php $function = Decorate::onAfter($function, $newFunction); ``` Where * ```$function``` is the callable function * ```$newFunction``` is the callable function that will be called after the other one.Note that the argument of this function is just the return of the other function * the method return a ```FunctionEmulator``` class ### 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: * ```getPreReturn()```: this function can be called with how many arguments you want and they are the same arguments of your main function to call. It will return the original function return. * ```getParent()```: this function will return the original function or older FunctionEmulator. In this way you can iterate on the history and find all the result of aggregated functions. ## 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: * ```OnAfterDecorativeLinkedList``` * ```OnBeforeDecorativeLinkedList``` 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: ```php 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 ```

  Files folder image Files  
File Role Description
Files folder imageADT (2 files)
Files folder imagecore (4 files)
Files folder imageexamples (4 directories)
Plain text file Decorate.php Class The main class
Accessible without login Plain text file LICENSE.mid Lic. License text
Accessible without login HTML file offline-doc.html Doc. doc
Accessible without login Plain text file README.md Doc. git documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:375
This week:2
All time:6,749
This week:204Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:72%StarStarStarStar
Rank:274
 
nice class!
9 years ago (Ovunc Tukenmez)
80%StarStarStarStarStar