Welcome Guest, Not a member yet? Register   Sign In
Clarification about MY_Controller and relative inheritances
#1

Hello there.

I need a clarification about the use of MY_Controller and their inheritances.

In my application I'm developing, I have a controller file named MY_Controller with a class

Code:
MY_Controller extends CI_Controller

In the same file I have others two classes, the first one:

Code:
Backend_Controller extends MY_Controller


and

Code:
Frontend_Controller extends MY_Controller

After, I have a series of other controllers extending the two previous classes.

And here I need the clarification: inside the construct function Backend_Controller, I call
a general model in this way:

Code:
Class Backend_Controller extends MY_Controller {
    public function __construct()
   {
       parent::__construct();
       $this->load->model('Auth_Model');
   }

after for example, I have:

Code:
Class Dashboard extends Backend_Controller {
    public function __construct()
   {
       parent::__construct();
   }

   public function index()
   {
       $content = $this->Auth_Model->is_logged_in();
   }
Inside the index function, I call a function contained by the Auth_Model called previously in Backend_Controller,
But I receive an error. I thought that for putting model call inside the constructor of class father, also class child inherits that call.

It works only if I call another time the model inside the function:

Code:
Class Dashboard extends Backend_Controller {
    public function __construct()
   {
       parent::__construct();
   }

   public function index()
   {
       $this->load->model('Auth_Model');
       $content = $this->Auth_Model->is_logged_in();
   }

What's wrong? My knowledge about the inheritance or I'm wrong something in the code?

Just for to learn more about.

Thanks in advance

Giorgio
Reply
#2

What is the exact error you get?

I see nothing wrong with your understanding of inheritance. I don't see anything wrong with your code either. I constructed a test case doing the same as you (as I understand it) the test works just fine.

File: application/core/MY_Controller.php

PHP Code:
class MY_Controller extends CI_Controller
{
 function 
__construct()
 {
 
parent::__construct();
 }
}

class 
Backend extends MY_Controller
{

 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->model('rivers_m');
 
   }



The model is one I had handy that is known to work.

File: application/controllers/Welcome.php

PHP Code:
class Welcome extends Backend
{
 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

 
   public function index()
 
   {
 
       $data $this->rivers_m->getRivers();
 
       var_dump($data);
 
   

The var_dump produces the expected output.
Reply
#3

You need a way to load your frontend and backend controller. I still rely on this old link by Phil Sturgeon to add some code to the config.php fle which autoloads your files.

https://philsturgeon.uk/codeigniter/2010...ng-it-DRY/

Or you could put both classes inside the regular MY_controller file. Personally I'm not a big fan of creating a single file with multiple porposes.
Reply
#4

(This post was last modified: 04-17-2017, 02:40 PM by marksman.)

I found no problem with your structure, unless you where missing the case of your files. It is because your model class is in Studly_Snake case and if your file is not studly_snake.php then you may encounter problems for some reasons, I'm not sure why but I'ved encounter those from the past. If your problem persist just to make sure you were inheriting the right object, try to store your object in global variable where it can be inherited by your class and it's child.

PHP Code:
Class Backend_Controller extends MY_Controller
{
 
   protected $oAuthModel;
 
   
    public 
function __construct()
 
  {
 
      parent::__construct();
 
      $this->load->model('Auth_Model');
 
      $this->oAuthModel $this->Auth_Model;
 
      unset($this->Auth_Model);
 
  }


PHP Code:
Class Dashboard extends Backend_Controller
{
 
   public function __construct()
 
   {
 
      parent::__construct();
 
   }

 
   public function index()
 
   {
 
      $content $this->oAuthModel->is_logged_in();
 
   }


and I missed it @Diederick is right, your controller might not be loaded properly. It is either you follow MY_Controller convention or change the $config['subclass_prefix'] = 'MY_'; in your config.php plus you may write your own autoload class in MY_Controller convention if you don't want to change your config.
God Bless CI Contributors Smile
Reply
#5

(This post was last modified: 04-17-2017, 02:52 PM by dave friend.)

@Diederik and  @marksman,

The OP already has all this child classes in the same file as MY_Controller so he has no need of an autoloader. Not that I agree with that technique but it does work.

His Dashboard class seems to load (at least his post implies it does) so that means all the parent classes are being constructed. Makes me wonder if the code he is actually running has a typo. I don't see one in the code he presented. The exact error message may shed some light things.

As best I can tell the test case I posted earlier recreates the structure Giorgio describes and it works perfectly.
Reply
#6

I believe looking at what he posted this is missing in the constructor of MY_Controller

PHP Code:
parent::__construct(); 

Or maybe the whole constructor method is missing in the MY_Controller class
Reply
#7

(This post was last modified: 04-17-2017, 04:38 PM by marksman.)

@Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router.

PHP Code:
class Dashboard extends MY_Controller
{
    public function 
index()
    {
        
// If this returns an error then your Backend_Controller is not loaded
        
$oBackend = new Backend_Controller();
    }


You extend My_Controller to CI_Controller while Backend_Controller extends MY_Controller. I think you missed it my friend. No one extends or instantiate Backend_Controller so no chance to access it's instance, your Dashboard also extends to MY_Controller. In your structure MY_Controller stands as your base class neither Backend_Controller nor Frontend_Controller. That's why.
God Bless CI Contributors Smile
Reply
#8

(04-17-2017, 02:56 PM)Martin7483 Wrote: I believe looking at what he posted this is missing in the constructor of MY_Controller

PHP Code:
parent::__construct(); 

Or maybe the whole constructor method is missing in the MY_Controller class

But, if MY_Controller does not mess with any CI_Controller properties (and it only has one - private static $instance; so there is nothing to mess with) then technically he does not need to explicitly call the base class constructor. PHP will do it anyway.
Reply
#9

(04-17-2017, 04:34 PM)marksman Wrote: @Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router.

PHP Code:
class Dashboard extends MY_Controller
{
 
   public function index()
 
   {
 
       // If this returns an error then your Backend_Controller is not loaded
 
       $oBackend = new Backend_Controller();
 
   }


You extend My_Controller to CI_Controller while Backend_Controller extends MY_Controller. I think you missed it my friend. No one extends or instantiate Backend_Controller so no chance to access it's instance, your Dashboard also extends to MY_Controller. In your structure MY_Controller stands as your base class neither Backend_Controller nor Frontend_Controller. That's why.

I think you're reading it wrong. Look at my code again.

  1. MY_Controller extends CI_Controller;
  2. Backend extends MY_Controller;
  3. Welcome extends Backend;
I substituted "Welcome" for the OP's "Dashboard" cause I was too lazy to create another class. Angel

The inheritance chain will instantiate Backend.

I know "Backend" was created because the object "river_m" is successfully used in "Welcome" to return data from the db.

In my example I did not define a Frontend class but if I did it would extend MY_Controller. Smile
Reply
#10

(04-17-2017, 06:41 PM)dave friend Wrote:
(04-17-2017, 04:34 PM)marksman Wrote: @Dave friend, have you tried to var_dump in Backend_Controller's constructor? I'm afraid its not loaded. or try to instantiate it in your class Dashboard which is basically loaded by magic router.

PHP Code:
class Dashboard extends MY_Controller
{
 
   public function index()
 
   {
 
       // If this returns an error then your Backend_Controller is not loaded
 
       $oBackend = new Backend_Controller();
 
   }


You extend My_Controller to CI_Controller while Backend_Controller extends MY_Controller. I think you missed it my friend. No one extends or instantiate Backend_Controller so no chance to access it's instance, your Dashboard also extends to MY_Controller. In your structure MY_Controller stands as your base class neither Backend_Controller nor Frontend_Controller. That's why.

I think you're reading it wrong. Look at my code again.

  1. MY_Controller extends CI_Controller;
  2. Backend extends MY_Controller;
  3. Welcome extends Backend;
I substituted "Welcome" for the OP's "Dashboard" cause I was too lazy to create another class. Angel

The inheritance chain will instantiate Backend.

I know "Backend" was created because the object "river_m" is successfully used in "Welcome" to return data from the db.

In my example I did not define a Frontend class but if I did it would extend MY_Controller. Smile

Oh I see, My bad.

If that the case then would you think how CI include the file of backend/frontend controller without modifying config.php from MY_ convention to your own or by creating your own autoloader inside MY_Controller
God Bless CI Contributors Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB