Welcome Guest, Not a member yet? Register   Sign In
should I not use __construct()
#1

[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?
#2

[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();
  }
}
#3

[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.
#4

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

[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?
#6

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

[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 :-)




Theme © iAndrew 2016 - Forum software by © MyBB