PHP Classes
elePHPant
Icontem

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 (4 years ago) RSS 2.0 feedStarStarStarStar 73%Total: 884 All time: 3,898 This week: 387Up
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 <contact>
Classes: 6 packages by
Country: Germany Germany
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:884
This week:0
All time:3,898
This week:387Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:91%StarStarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:73%StarStarStarStar
Rank:211