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)    
Last Updated Ratings Unique User Downloads Download Rankings
2014-06-23 (2 years ago) RSS 2.0 feedStarStarStarStar 73%Total: 878 All time: 3,846 This week: 655Up
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
  Performance   Level  
Name: Fabian Schmengler is available for providing paid consulting. Contact Fabian Schmengler .
Classes: 6 packages by
Country: Germany Germany
Age: 34
All time rank: 105467 in Germany Germany
Week rank: 1098 Down53 in Germany Germany Up
Innovation award
Innovation award
Nominee: 4x

|                             Decorate Anything                               |

- Synopsis
- Requirements
- Files
- Usage

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.

The package requires PHP 5.2.6 or later.

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


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()

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->bar(1,2); // assuming a method ConcreteComponent::bar($x,$y)
	var_dump($component->baz); // assuming a property ConcreteComponent::$baz
	$component->baz = 1;

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()
			// 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  
This week:0
All time:3,846
This week:655Up
User Ratings User Comments (1)
 All time