1

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
JamesM-SiteGen
  • 802
  • 2
  • 11
  • 26
  • I want to have a mods directory that may have mods enabled and may not have them enabled. I can't really extend one class, then extend that one more, and make sure that all modifications are changed. – JamesM-SiteGen Jan 19 '11 at 06:08
  • What would be great would be if you could run `class test extends test {...}` – JamesM-SiteGen Jan 19 '11 at 06:09
  • Have you got any other methods? – JamesM-SiteGen Jan 19 '11 at 06:32
  • possible duplicate of [Is it possible to dynamically add code to/extend a class?](http://stackoverflow.com/questions/4668822/is-it-possible-to-dynamically-add-code-to-extend-a-class) – Gordon Jan 19 '11 at 08:46
  • @Gordon, humm, correct but incorrect, they are asking to add things into a class dynamic, I am asking to merge, Similar but not the same. Merging will allow me to override functions while keeping the originals, Okay the answer they posted happens to be similar to my solution, but my solution does not need me to register the class into the original, instead my solution takes in all from an existing array, both work and both are good methods, I will post my answer in the "possible Duplicate". They are not the same, I will edit my question to answer why. – JamesM-SiteGen Jan 19 '11 at 12:49
  • @James I was undecided between just giving it as related or as a possible duplicate. I decided to provide it as possible dup because I think I would have answered the same as to that one to your question. – Gordon Jan 19 '11 at 13:02
  • With the answer from your "possible duplicate" I would of still needed to modify lots of its code as it is made to be ran after the class has constructed, What I need is to add the mods during construction. Okay, very similar questions, but I would not say duplicate. We are asking for each side of the construction :) Me for before construction and savetheinternet is after calling a function after we have already constructed the main_class. – JamesM-SiteGen Jan 19 '11 at 13:43
  • As both our needs are to mod our systems, but What I need is to modify the main_class before I construct my main_class, and savetheinternet wanted to later call in his modules. – JamesM-SiteGen Jan 19 '11 at 13:45

2 Answers2

0

In C# you would do this by defining a partial class. It's basically like writing more of the same class in a different file. I'm not sure if PHP supports this, but this article may help?

http://www.toosweettobesour.com/2008/05/01/partial-classes-in-php/

jocull
  • 20,008
  • 22
  • 105
  • 149
0

Self-Solved :) I have figured this out on my own and I'm sure someone else will find it useful so here is my php 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:

$MODS_ENABLED=array();
class mod_mail {...}
$MODS_ENABLED[]=new mod_mail;

Now all you need to do is just call your main class via.

$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 this was not my usage but I'm sure you get the idea, My class is a CMS-back-end and my mod was a redirecting a few sub-domains to other locations, such as mail.sitegen.com.au and phpmyadmin.sitegen.com.au get redirected to there web-gui's out side of my CMS. I am also making more mods for my CMS.

BTW: my extensions are split into two categories, mods and plugins, mods run on every site that is powered by my CMS (SiteGen) and plug-ins have there own settings for each site and they are not required. In other words mods are chosen by me, and plug-ins are chosen by the owner of the site.

JamesM-SiteGen
  • 802
  • 2
  • 11
  • 26