CodeIgniter Forums
should I not use __construct() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: should I not use __construct() (/showthread.php?tid=17255)



should I not use __construct() - El Forum - 03-30-2009

[eluser]Flying Fish[/eluser]
Up to this point when I create a class I have been using __construct, since I know my host is running php5

I found that it had some issues when extending the controller class with MY_Controller

Should I pretty much always use the same function name as the class for my constructor functions

like this...
Code:
class MY_Controller extends Controller {

   function MY_Controller()

not this...
Code:
class MY_Controller extends Controller {

   function __construct()

are there any drawbacks to this approach?


should I not use __construct() - El Forum - 03-30-2009

[eluser]Rick Jolly[/eluser]
Always use __construct() with php 5.

Just be aware that CI classes are php 4 compatible, so constructors use the class name. Therefore, when calling a CI parent controller within your extending class you must use parent::ClassName();

Code:
class MY_Controller extends Controller
{
  function __construct()
  {
    // not parent::__construct() with CI php 4 classes
    parent::Controller();
  }
}



should I not use __construct() - El Forum - 03-31-2009

[eluser]xwero[/eluser]
Rick you can use __construct with the php4 constructor, php5 is smart enough to figure it out.

If you want backwards compatibility you can do
Code:
function MY_Controller()
{
   $this->__construct();
}
php5 will fetch the __construct method.


should I not use __construct() - El Forum - 03-31-2009

[eluser]Rick Jolly[/eluser]
xwero, thanks you are correct. I thought I had problem when trying that, but it must have been something else.


should I not use __construct() - El Forum - 03-31-2009

[eluser]Flying Fish[/eluser]
thanks, let me see if I've got this right

so in MY_Controller is do this...
Code:
class MY_Controller extends Controller {

   function __construct()
   {
      parent::Controller();
      
      $this->load->vars('head', $this->load->view('_inc/head', '', TRUE));
      $this->load->vars('masthead', $this->load->view('_inc/masthead', '', TRUE));
      $this->load->vars('footer', $this->load->view('_inc/footer', '', TRUE));

   }

}


/*
    End of file MY_Controller.php
    Location: /system/application/libraries/MY_Controller.php
*/



and in all my other conrollers I do this...
Code:
class Order extends MY_Controller{

    function __construct()
    {
        parent::__construct();

    }
    

    function index()
    {



is that right?


should I not use __construct() - El Forum - 04-02-2009

[eluser]The Wizard[/eluser]
thıs works for the controller but the model seems not to be working properly with that


should I not use __construct() - El Forum - 04-02-2009

[eluser]Flying Fish[/eluser]
I think I need to go watch a lynda.com tutorial on how to extend php5 classes, it would be good for me anyway :-)