This article describes the implementation and usage of PHP simple decorator mode. Share it for your reference, as follows:
<?php //Decorator mode - supplements the function of the class without changing the original class structure//Weapon baseabstract class Weapon{ abstract public function descriptions(); abstract public function cost(); } //Swordsclass Glave extends Weapon{ public function descriptions(){ return 'Glave'; } public function cost(){ return "100"; } } //Daughter typeclass Knife extends Weapon{ public function descriptions(){ return __CLASS__; } public function cost(){ return "80"; } } //Axeclass Axe extends Weapon{ public function descriptions(){ return __CLASS__; } public function cost(){ return "200"; } } //Propertiesclass 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; } public function descriptions(){ return $this->_weapon->descriptions().$this->_descriptions; } } //Strength attributeclass Strength extends Property{ protected $_price = 30; protected $_descriptions = '+ Strength'; } //Agile Propertiesclass Agility extends Property{ protected $_price = 50; protected $_descriptions = '+ Agility'; } //Intellectual Attributesclass Intellect extends Property{ protected $_price = 20; protected $_descriptions = '+ Intellect'; } $weapon = new Agility(new Strength(new Strength(new Glave()))); echo $weapon->cost(); echo $weapon->descriptions();
For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Introduction to PHP basic syntax》、《Summary of PHP network programming skills》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.