⬆️ ⬇️

Class plugging

Somehow, while meditating on his new bike, he thought, “Why not lay in it the possibility of“ plug-in ”controllers?



So that, for example, we have a certain base class Generic.php :

<?php class Generic { public function Hello() { echo 'Hello!'; } } ?> 




And we would take such and hang on it a couple of plug-ins that will add / change any of its methods without interfering with each other (according to the principle “who first got up - that and sneakers”).

')

Here they are handsome:

PluginFoo.php :

 <?php class PluginFoo extends Generic_PluginFoo { public function Hello() { echo 'Dudes?<br />'; parent::Hello(); } } ?> 


and PluginBar.php :

 <?php class PluginBar extends Generic_PluginBar { public function Hello() { parent::Hello(); echo '<br />O.o'; } } ?> 




And then we would create an object of the parent class (without referring to the last descendant), call the Hello () method and he would tell us:

Dudes?

Hello!

Oo




Below is a concise version of how the described behavior has achieved ort in his LiveStreet , and then I in my own bicycle.



You probably already noticed in the code above that PluginFoo and PluginBar plugin classes are inherited from the undescribed Generic_PluginFoo and Generic_PluginBar classes, respectively? So they will not be at all. We need this feint with our ears for classifying and building the chain of inheritance.



Yes, what rubber pull.

Here is an example of use:

 //   (  ) $KungFu = new KungFu(); //   PluginFoo    Generic $KungFu->RegisterPlugin('Generic', 'PluginFoo'); //   PluginBar    Generic $KungFu->RegisterPlugin('Generic', 'PluginBar'); //  <i></i>   Generic $Generic = $KungFu->Load('Generic'); $Generic->Hello(); 


// Get

// Dudes?

// Hello!

// oo



Magic tool:

 class KungFu { // ...  public function __construct() { spl_autoload_register(array($this, '_AutoLoader')); } // ...   public function _AutoLoader($sClass) { if (false == class_exists($sClass)) { include($sClass.'.php'); } } //      // Array ( [Generic] => Array ( [0] => PluginFoo [1] => PluginBar ) ) private $_aPlugins = array(); //   ,  ,    public function RegisterPlugin($sClass, $sPlugin) { if (false == isset($this->_aPlugins[$sClass])) { $this->_aPlugins[$sClass] = array(); } array_push($this->_aPlugins[$sClass], $sPlugin); } //     .      ,       . public function Load($sClass) { if (false == isset($this->_aPlugins[$sClass])) { return new $sClass(); } else { $aPlugins = array_reverse($this->_aPlugins[$sClass]); $sPrev = null; foreach ($aPlugins as $sPlugin) { if (null != $sPrev) { $aBranch[$sPrev] = $sPlugin; } $aBranch[$sPlugin] = null; $sPrev = $sPlugin; } $aBranch[$sPrev] = $sClass; foreach (array_reverse($aBranch) as $sPlugin => $sParent) { class_alias($sParent, $sClass.'_'.$sPlugin); } if (class_exists($sPlugin)) { return new $sPlugin; } } } } 




And now I have a question, - What is it called in the “language of patterns”? What would you call it?



PS Of course - this post is a consequence of the April Fools ’draw with PPA :)





- TA-dah!

Source: https://habr.com/ru/post/116713/



All Articles