PHP Classes

Decorate Anything: Implement the decorator design pattern

Recommend this page to a friend!
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog (1)    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 65%Total: 899 This week: 1All time: 3,932 This week: 571Up
Version License PHP version Categories
decorate-anything 1.0BSD License5.2.6PHP 5, Design Patterns
Description 

Author

This class implement the decorator design patterns.

It implements a base abstract class that should be extended by decorator classes that wrap the result of the wrapped class objects.

When the decorator classes accesses the base decorator functions or variables, the abstract decorator class redirects the access to the wrapped class objects.

Innovation Award
PHP Programming Innovation award nominee
December 2009
Number 5


Prize: One book of choice by Packt
Decorator is a design pattern intended to change the outcome of programming component using another independent component.

This class provides a generic PHP implementation of the decorator design pattern allowing to wrap the functionality of any object with decorator classes without having to inherit from the wrapped object classes.

Manuel Lemos
Picture of Fabian Schmengler
  Performance   Level  
Name: Fabian Schmengler is available for providing paid consulting. Contact Fabian Schmengler .
Classes: 6 packages by
Country: Germany Germany
Age: 41
All time rank: 105265 in Germany Germany
Week rank: 420 Up17 in Germany Germany Up
Innovation award
Innovation award
Nominee: 4x

Details

+-----------------------------------------------------------------------------+ | Decorate Anything | +-----------------------------------------------------------------------------+ - Synopsis - Requirements - Files - Usage Synopsis -------- This package is a simplified implementation of the Decorator Design Pattern. Sub classes of the provided AbstractDecorator class may be used to decorate any objects without the need for abstract components and abstract decorators for each of them thanks to PHP's magic methods and loose typification. Requirements ------------ The package requires PHP 5.2.6 or later. Files ----- decorateanything.lib.php - library loader, include this file to use the package AbstractDecorator.php - the AbstractDecorator class example.php - "Hello World" example to demonstrate simple usage license.txt - BSD License readme.txt - the file you are reading right now Usage ----- To create a decorator for a component, say ConcreteComponent, just extend the AbstractDecorator like this: class ConcreteDecorator extends AbstractDecorator { const COMPONENT_CLASS = 'ConcreteComponent'; } To extend functionality of a component method in the decorator, use the parent keyword like you would do in the original decorator pattern: public function foo() { parent::foo(); some_special_functionality(); } Now you can decorate your component like this: $component = new ConcreteDecorator(new ConcreteComponent()); $component will have the same interface as ConcreteComponent, you also can access the public properties of the decorated component. $component->foo(); $component->bar(1,2); // assuming a method ConcreteComponent::bar($x,$y) var_dump($component->baz); // assuming a property ConcreteComponent::$baz var_dump(isset($component->baz)); $component->baz = 1; unset($component->baz); If you really have to name the common interface of decorator and compontent explicitly do it this way: interface IConcreteComponent { public function foo(); public function bar($x,$y); } class ConcreteComponent implements IConcreteComponent { public $baz = 'baz'; public function foo() { // implementation } public function bar($x,$y) { // implementation } } class ConcreteDecorator extends AbstractDecorator implements IConcreteComponent { const COMPONENT_CLASS = 'ConcreteComponent'; public function foo() { parent::foo(); // additional implementation } public function bar($x,$y) { return parent::bar($x,$y); } }

  Files folder image Files  
File Role Description
Plain text file decorateanything.lib.php Aux. Include this file to use the package
Plain text file AbstractDecorator.php Class The AbstractDecorator class
Accessible without login Plain text file example.php Example "Hello World" example to demonstrate simple usage
Plain text file license.txt Lic. BSD License
Plain text file README Data Github README
Accessible without login Plain text file readme.txt Doc. Short class description

 Version Control Unique User Downloads Download Rankings  
 16%
Total:899
This week:1
All time:3,932
This week:571Up
User Ratings User Comments (1)
 All time
Utility:81%StarStarStarStarStar
Consistency:81%StarStarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:81%StarStarStarStarStar
Tests:-
Videos:-
Overall:65%StarStarStarStar
Rank:644