Welcome Guest, Not a member yet? Register   Sign In
Extending a controller with __construct not working
#1

[eluser]flaky[/eluser]
I don't know how to ask better so I'll try to be as clear as possible

I'm using HMVC and Template Library

I have this controller which I want every other controller to extend, I know I could have used MY_Controller, but I prefer it this way.

Path of main.php - /system/application/controllers/main.php
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Main extends Controller{
    
    public function Main(){
        parent::Controller();
        
        $this->output->enable_profiler(TRUE);
    }
    
    public function index(){
    }
    
}

An example of another controller extending the Main controller

path of user.php - /system/application/modules/user/controllers/user.php
Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

require_once(BASEPATH . 'application/controllers/main' . EXT);

class User extends Main{
    
    public function User(){
        parent::Main();
    }
    
    public function index(){
    }

}

As you can see from sample code, like this it works without a problem, but the moment I change the constructors to "__construct()" it is just giving me the 500 error.

I just want to make my app as PHP5 friendly as possible. Is there a solution to this (which I'm not aware of) ?.

Thanks in advance.
#2

[eluser]überfuzz[/eluser]
I guess you've checked, but are you sure you're running on php5..?
#3

[eluser]flaky[/eluser]
Yes
Running on PHP 5.3
#4

[eluser]flaky[/eluser]
There is sth strange. The controllers extending main can have their constructors like "__construct()" or "User()" but the Main controller only works with "Main()" constructor and not with "__construct()".
Strange.
#5

[eluser]frist44[/eluser]
Did you try using it for the parent call too? I don't know if this is the problem, but I'm looking at my MY_Controller.php and it has something like this:

Code:
class Main extends Controller{
    
    function __construct(){
        parent::__construct();
        
    }
#6

[eluser]theprodigy[/eluser]
if the child class is calling
Code:
parent::__construct();

then the constructor of the parent needs to be in __construct() format, not { className }() format (as far as I know).

If you change your child to call
Code:
parent::__construct();
Change the name of your constructor in main to
Code:
public function __construct()

Try making all your constructors (from children controllers on up to -and including- main)
Code:
public function __construct(){
    parent::__construct();
}
#7

[eluser]flaky[/eluser]
Yep, this was the solution.
Should have realized it myself.

Thanks. :-)




Theme © iAndrew 2016 - Forum software by © MyBB