views:

530

answers:

2

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  A: 

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);
    }
}
xtofl
but the parameter ($uri) that is needed by all three classess in my question's code is supplied to the base class constructor.will this be directly available in childest class constructor
OrangeRind
oh right! so don't the constructors cascade like in Java/C++ or is the article on your link only for cases where parametrized constructors are involved? So that means first the default constructors are loaded and then when control returns back to the parent class, we invoke the child class parametrized constructor explicitly?
OrangeRind
That's right. The link in middus' answer (to the authorative PHP docs - take a look!) shows this comment, too: "Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required"
xtofl
+3  A: 

I think you should add calls to the parent constructor as described in http://php.net/manual/en/language.oop5.decon.php

class SubClass extends BaseClass {
  function __construct() {
    parent::__construct();
    print "In SubClass constructor\n";
  }
}
middus
but the parameter ($uri) that is needed by all three classess in my question's code is supplied to the base class constructor.will this be directly available in childest class constructor
OrangeRind