This paper mainly introduces the PHP simple decorator pattern implementation and usage, combined with concrete example form analysis of the PHP decorator mode principle, implementation and use of the method, the need for friends can refer to, hope to help everyone.
<?php//Decorator mode-on the structure of the original class, the function of the class is supplemented//weapon-based class abstract class weapon{abstract public function descriptions (); Abstract public function cost ();} Sword class Glave extends weapon{public Function descriptions () {return ' Glave '; } Public Function cost () {return ' 100 '; }}//Dagger class Knife extends weapon{public Function descriptions () {return __class__; } Public Function cost () {return ' 80 '; }}//Axe class Axe extends weapon{public Function descriptions () {return __class__; } Public Function cost () {return ' 200 '; }}//Attribute Class Property extends weapon{protected $_weapon = null; protected $_price = 0; protected $_descriptions = "; Public function __construct (weapon $weapon) {$this->_weapon = $weapon; } Public Function cost () {return $this->_weapon->cost () + $this->_price; The Public Function descriptions () {return $this->_weapon->descriptions (). $this->_descriptions; }}//Power Attribute Class Strength extends property{protected $_price = 30; Protected $_descriptions = ' + Strength ';} Agile Attribute Class Agility extends property{protected $_price = 50; Protected $_descriptions = ' + Agility ';} Intellectual properties Class Intellect extends property{protected $_price = 20; Protected $_descriptions = ' + Intellect ';} $weapon = new Agility (New Strength (new Glave ())), Echo $weapon->cost (), Echo $weapon->descriptions () ;