Download .zip |
Info | View files (6) | Download .zip | Reputation | Support forum (1) | Blog (1) | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2014-06-23 (4 years ago) | 73% | Total: 884 | All time: 3,898 This week: 387 |
Version | License | PHP version | Categories | |||
decorate-anything 1.0 | BSD License | 5.2.6 | PHP 5, Design Patterns |
Description | Author | |||
This class implement the decorator design patterns. Innovation Award
|
+-----------------------------------------------------------------------------+ | 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 |
File | Role | Description |
---|---|---|
decorateanything.lib.php | Aux. | Include this file to use the package |
AbstractDecorator.php | Class | The AbstractDecorator class |
example.php | Example | "Hello World" example to demonstrate simple usage |
license.txt | Lic. | BSD License |
README | Data | Github README |
readme.txt | Doc. | Short class description |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
16% |
|
|
User Ratings | User Comments (1) | ||||||||||||||||||||||||||||||||||
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Related pages |
Decorator Pattern Original pattern explained in Wikipedia |