douxiong1994 2009-06-24 07:24
浏览 36
已采纳

参数化构造函数级联

Consider the following situation

class URISplit {

    var $REQ_URI;

    //some more variables

    function __construct($uri) {
        //some code
        $this->REQ_URI = $uri;
        //some code yet again
    }
}

and the following

class URIResolve extends URISplit {

    //some variables

    function __construct($uri) {
        //some code
    }
}

and another

class PageControl extends URIResolve {

    //some variables

    function __construct($uri) {

        //some more code

    }
}

and now the following statement

$page = new PageControl($_SERVER['REQUEST_URI']);

will this statement ensure the proper construction of all classes.

In other words, will constructors of class URISplit and class URIResolve use the string supplied to class PageControl's constructor, and do the proper construction.

My Objective is to just create an object of class PageControl and relax and see it do the work. Work means ->

  1. splitting the URI (done by class URISplit)
  2. resolving it (where to fetch the data for what is asked i.e. whether its a post, page, news, or anything else) (done by class URIResolve)
  3. loading appropriate headers, pages, and other page components (done by functions in class PageControl

Phew! Long question!

  • 写回答

2条回答 默认 最新

  • drv16821 2009-06-24 07:32
    关注

    You should explicitly call the parent constructor from the child's one. Take a look at this site for an example.

    The reason is that the child class may choose to give the parent constructor some different arguments.

    Heck, it's even possible to have an

    class SOSplit extends URISplit {
        function __construct() { 
            parent::__construct( "http://stackoverflow.com" );
        }
    }
    
    class URIResolve extends URISplit {
    
        //some variables
    
        function __construct($uri) {
            parent::__construct( $uri);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路