I was wondering if it was a way to get what you would see as
class main_class extends main_class {...}
But php was not happy. :(
So then I though to myself lets ask stackoverflow, I'm sure someone will know a solution. Never the less several hours of debugging I self-solved my problem, with only a little code.
The problem was the fact of class some_class
won't let you override an existing class so what I needed to do was use __get and __call and add another 2 lines into my __construct function.
So here is my solved-code:
class main_class {
private $_MODS = array(),...;
public ...;
public function __construct(...) {
...
global $MODS_ENABLED;
$this -> $_MODS = $MODS_ENABLED;
}
...
public function __get( $var ) {
foreach ( $this->_MODS as $mod )
if ( property_exists( $mod, $var ) )
return $mod -> $var;
}
public function __call( $method, $args ) {
foreach ( $this->_MODS as $mod )
if ( method_exists( $mod, $method ) )
return call_user_method_array( $method, $mod, $args );
}
}
Then simply run this to extend my main_class without overriding the original functions, so it has me run my new functions but if I need to I can get the original functions:
$MODS_ENABLED=array();
class mod_mail {...}
$MODS_ENABLED[]=new mod_mail;
Now lets load our class and run a function from our mod:
$obj = new main_class(...);
$obj -> mail("root@localhost", "me@me.me", "Testing a mod.", "This email was sent via My main_class but this is a mod that extended main_class without renaming it.");
Okay well my mod was not for sending emails but instead redirects sub-domains to there aliased pathname, but you understand the concept shown here.
Edit: After I solved the issue I saw a comment saying a possible duplicate exists so I check it out and find out someone else has an extremely similar solution, but please don't mark it as a duplicate as he was asking about adding to a class that was already constructed, I want to override functions while constructing. My solution takes in an array of constructed classes and "merges" them into my main_class, This method does reserve the original functions but I can also call the original functions using another function to by-pass the __call function.
Thanks to anyone who posted answers.