My plugin is going to be pretty big and it is going to have a lot of class. What I'm trying to do is create a parent class for a specific group of class. It works to a certain extent but not good enough really. What I have right now would be handy for very specific procedures that involve creating a string result using functions in multiple class. Possibly handy but I was hoping PHP would allow better than this and can't see that it does. But I ain't an expert.
I'm wondering if anyone can help get around my problem or confirm that what I'm trying to do is impossible, please.
The problem is the use of _toString() and not being able to either remove it or use it practically in very large class. When I say large I simply mean a few functions more than this example so not really large right, just normal.
Abstract class WPeCUS_ExtensionBridge
{
// array containing all the extended classes
private $_exts = array();
public $_this;
function __construct(){$_this = $this;}
public function addExt($object)
{
$this->_exts[]=$object;
}
public function __get($varname)
{
foreach($this->_exts as $ext)
{
if(property_exists($ext,$varname))
return $ext->$varname;
}
}
public function __call($method,$args)
{
foreach($this->_exts as $ext)
{
if(method_exists($ext,$method))
return call_user_method_array($method,$ext,$args);
}
throw new Exception("This Method {$method} doesn't exists, not good really. Please report this problem, thank you.");
}
}
class WPECUS_WP_to_phpBB_bridge {
public function doesthisworkwell2(){
return 'Sending some data from Wordpress to the phpBB database, we will want a return on it, boolean, string whatever.';
}
}
class WPECUS_userssection_to_phpBB_bridge {
public function doesthisworkwell1(){
return 'The user section of our pluginn and phpBB itself is pretty big so we are using this class to deal with user data specifically, how about returning arrays?';
}
}
/**
* Main bridge class
* Offers integration functionality across WP e-Customer sections and 3rd party software. Currently integrated...
*
* Each section has its own class which extends this class
*/
class WPeCus_bridges_main extends WPeCUS_ExtensionBridge {
function __construct()
{
parent::addExt(new WPECUS_WP_to_phpBB_bridge());
parent::addExt(new WPECUS_userssection_to_phpBB_bridge());
}
public function __toString()
{
return $this->doesthisworkwell1().', from: '.$this->doesthisworkwell2();
}
}
$o = new WPeCus_bridges_main();
$o->doesthisworkwell1();
echo $o;
$o->doesthisworkwell2();
echo $o;
So that is pretty much it but just to be clear. Here is the specific issue that makes me wonder if this approach is going to work.
return $this->doesthisworkwell1().', from: '.$this->doesthisworkwell2();
Can this line be replaced with anything that would allow a return from any function rather than stating them? This line pretty much renders all the code above as a very specific procedure rather than being a general one. It's almost useless right now really and my knowledge of PHP class ends here. I know all about using "extends" and I know multiple class can be stacked to get the effect I'm looking for. Problem there is if one class is not loaded it breaks the chain and that is likely in our plugin as we plan for it to be split into addons.
Any ideas, providing your making sense of this right!?