开发者

How to extend a php class with more classes

开发者 https://www.devze.com 2023-02-06 00:06 出处:网络
I was wondering if it was a way to get what you would see as class main_class extends main_class {...}

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(),...;
    pu开发者_运维问答blic ...;
    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.


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/


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消