0

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')

6 Answers 6

4

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.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Upgrade to CI 2.0 for proper use of __construct() all the way through or some versions of PHP will not handle the constructors properly. Some versions can have __construct() map to PHP4-style Controller, but CI 2.0 just removes all PHP 4 style constructors.
2

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!

Comments

2

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
    }
}

Comments

0

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

Comments

0

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.

Comments

0
class Blog extends CI_Controller {

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.