vikmalhotra
vikmalhotra

Reputation: 10071

Problem regarding use of constructors in PHP

My problem is in using Codeigniter custom library but I think it is not specific to that and more related to use of constructors in PHP. I am trying to create a custom controller library in Codeigniter like this...

class MY_Controller extends Controller {
    var $data = array();

    function MY_Controller() {
        parent::Controller();
        $this->data['err'] = 'no';

        $this->load->helper('utilities');
    }
}

Now I create a codeigniter controller class like this...

class api_controller extends MY_Controller {
    function get_data() {
        $this->data['view'] = "data";
        $this->load->view("data_view", $this->data);
    }
    function get_xml() {
        $this->data['part'] = "xml";
        $this->load->view("data_view", $this->data);
    }
}

I want to ask that if the controller class extending the MY_Controller is instantiated when I access urls like api_controller/get_data and api_controller/get_xml, does the constructor of parent class always get called upon, i.e., MY_Controller() is always called.

Am I correct in inferring the following

get_data() called
-> $data => array ('err' => 'no', 'view' => 'data')

get_xml() called
-> $data => array ('err' => 'no', 'part' => 'xml')

Upvotes: 0

Views: 475

Answers (6)

Prabhakar
Prabhakar

Reputation: 1

class Blog extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            // Your own constructor code
       }
}

Upvotes: 0

Carlos Mora
Carlos Mora

Reputation: 1184

You are right in your affirmations about the data array contents. In you code you wrote:

function MY_Controller() {
    parent::Controller();

so the parents's class constructor is being called. There are lots of comments about PHP4 and PHP5 syntax, but basically everithing is ok.

In your question you wrote

that if the controller class extending the MY_Controller is instantiated

that is not correct. The instance is an object of class api_controller, calling the MY_Controller constructor is made using the same object. That is not the same, that is basic for polimorphism.

Upvotes: 0

Spudley
Spudley

Reputation: 168755

Your code looks like it's using the PHP4 syntax for constructors. You should switch to the PHP5 syntax.

PHP4:

class MyClassName {
    function MyClassName() {...}  //constructor.
}

PHP5:

class MyClassName {
    function __construct() {...}  //constructor.
}

You can then call the constructor of a parent class by calling parent::__constructor(); from within the child class's __constructor() method:

class MyClassName extends SomeOtherClass {
    function __construct() {
        //...code here runs before the parent constructor.
        parent::__construct();
        //...code here runs after the parent constructor.
    }
}

Upvotes: 4

Jake N
Jake N

Reputation: 10583

If you do not override __construct() in MY_Controller then Controller's __construct() will get run.

If you override it and then called parent::__construct() then it will run your own and the parent's.

So if you wanted it to run the parent's and your own you would do this

class MY_Controller extends Controller 
{
    var $data = array();

    function __construct()
    {
        parent::__construct();
        // Your code here
    }
}

Upvotes: 2

Riccardo Galli
Riccardo Galli

Reputation: 12935

Yes, the parent constructor it is always called (you may want to rewrite them as __construct() however, thinking also at codeigniter 2.0 ). If you really are in doubt toss in it an echo and see it for yourself.

Also the $this->data part is correct to me

Upvotes: 0

Sondre
Sondre

Reputation: 1898

For PHP in general the parent constructor is not called default if the child has a constructor. Constructors. It can be called using parent::_construct();

If you're using php 5+ you should go with the new recommended style of using function __construct() instead of the old style with a function named the same as a class.

As for CI-specific stuff I can't help you, sorry!

Upvotes: 2

Related Questions