Is it wrong to oop-php class objects to inherit from each other extends? (Mutual inheritance)

Source: Internet
Author: User
/TxLibrary    /txOraApi        -oraBase.class.php        -oraNews.class.php         -oraPic.class.php    /txWebApi        -webBase.class.php        -webNews.class.php        -webPic.class.php    /txWapApi        -wapBase.class.php  :   class wapBase extends TxBase {}        -wapNews.class.php  :   class wapNews extends wapBase {}        -wapPic.class.php   :   class wapPic  extends wapBase {}    -TxBase.class.php   :  class TxBase {}    -TxNews.class.php   :  class TxNews extends TxBase {}    -TxPic.class.php    :  class TxPic extends TxBase {}

Description

This is my own idea, because there are three interfaces, Wap Ora web. So there are three class library folders created.
But the base class wapBase.class.php webBase.class.php oraBase.class.php the TxBase.class.php method between them. And TxNews.class.php may also use the TxBase.class.php method. So
TxNews.class.php extends TxBase.class.php, but in TxNews.class.php such a class library method, such as the GetList () method, is called oranews::getlist (), But Oranews extends Orabase, and Orabase extends Txbase.

Other words

class TxNews extends TxBase {    public function getList(){        return oraNews::getList();    }}class oraNews extends oraBase {    public function getList(){        $param = array();        return parent::getData($param);    }}class oraBase extends TxBase {    public function getData(){        //curl post remote java web api to get Data        return array();    }    }

Whether such inheritance is problematic, or that its own thinking is wrong.

Reply content:

/TxLibrary    /txOraApi        -oraBase.class.php        -oraNews.class.php         -oraPic.class.php    /txWebApi        -webBase.class.php        -webNews.class.php        -webPic.class.php    /txWapApi        -wapBase.class.php  :   class wapBase extends TxBase {}        -wapNews.class.php  :   class wapNews extends wapBase {}        -wapPic.class.php   :   class wapPic  extends wapBase {}    -TxBase.class.php   :  class TxBase {}    -TxNews.class.php   :  class TxNews extends TxBase {}    -TxPic.class.php    :  class TxPic extends TxBase {}

Description

This is my own idea, because there are three interfaces, Wap Ora web. So there are three class library folders created.
But the base class wapBase.class.php webBase.class.php oraBase.class.php the TxBase.class.php method between them. And TxNews.class.php may also use the TxBase.class.php method. So
TxNews.class.php extends TxBase.class.php, but in TxNews.class.php such a class library method, such as the GetList () method, is called oranews::getlist (), But Oranews extends Orabase, and Orabase extends Txbase.

Other words

class TxNews extends TxBase {    public function getList(){        return oraNews::getList();    }}class oraNews extends oraBase {    public function getList(){        $param = array();        return parent::getData($param);    }}class oraBase extends TxBase {    public function getData(){        //curl post remote java web api to get Data        return array();    }    }

Whether such inheritance is problematic, or that its own thinking is wrong.

Good around the problem, regardless of whether you are right or wrong, if you are now so difficult to clarify the problem, that in a few years or a change of a person, how to maintain the code?

I think it's not a good design even if it works.

If these classes require a common approach, inheritance is not the only way to implement it, and you can encapsulate these methods into stateless helpers, which are used in these classes separately.

Oranews::getlist () GetList () Now that you can allow static method calls, you can simply put them outside the class and package them separately.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.