Welcome Guest, Not a member yet? Register   Sign In
Extend CI_Controller class | Undefined method
#1

[eluser]WebbHelp[/eluser]
Hi!

I am trying to extend the CI_Controller class so I will be able to use methods in every controller class I creates.

MY_Controller.php | application/core/MY_Controller.php
Code:
<?php
class  MY_Controller  extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function testi()
    {
        echo 'AAAAAAAAAAAA';
    }
}
?>

I want to call the testi() method but I can't because I got error when I use this code:

Gallery.php | application/controllers/Gallery.php

Code:
class Gallery extends CI_Controller
{
    function __construct()
    {
        parent::MY_Controller();
    }
    
    public function index()
    {
        $this->testis();
    }
}

I got the error in the gallery file:
Quote:Fatal error: Call to undefined method CI_Controller::MY_Controller()

I don't know the problem but I hope you have an idea about fixing it!
I have not edit any config files... good to know?

Thanks!
#2

[eluser]maltzurra[/eluser]
I don't want to sound rude at all, but you should learn OOP basics first.

What you are looking for is:

Code:
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function testis()
    {
        echo 'AAAAAAAAAAAA';
    }
}



Code:
class Gallery extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
    }
    
    public function index()
    {
        $this->testis();
    }
}

Also note difference between testi() and testis()
#3

[eluser]WebbHelp[/eluser]
Thank you Smile
It worked perfectly!

I am ashamed about the testis() and testi(), ridiculous error...!

But you are right I am not good at OOP:
BEFORE: class Gallery extends CI_Controller
AFTER : class Gallery extends MY_Controller

I am actually not sure why I am supposed to use the last one. Of course because it works but...
Is it MY_Controller which extends the Gallery or the Gallery which extends MY_Controller?

And MY_Controller extends CI_Controller(I think?) so why can't I extend with CI_Controller?
Hope you understand my question Smile

Thanks
#4

[eluser]maltzurra[/eluser]
CI_Controller has core Controller functionalities. All Controllers that extend CI_Controller, will have these funcionalities.

For example, in your case:

MY_Controller extends CI_Controller. MY_Controller can do everything CI_Controller does besides the additional testis() method. CI_Controller on the other hand, can't call MY_Controller's own methods.

CI_Controller, can NOT call testis(), as it is MY_Controller's method. This said, if Gallery extends CI_Controller, you will NOT have MY_Controller functionalities and in short, you will not be able to call testis().

If you make Gallery extend MY_Controller, Gallery will have CI_Controller+MY_Controller+own functionalities.

This is called inheritance.

Hope it helps.
#5

[eluser]WebbHelp[/eluser]
Hope it helps?

It helped a lot Big Grin

Thanks!
I really understand it know... it is pretty simple actually.

Thanks for your time, everything works perfect now =)

This is the result:

Code:
<?php

class MY_Controller  extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function template($view, $view_data = array(), $template_data = array())
    {
        $this->load->view('header');
        $this->load->view($view, $view_data);
        $this->load->view('footer');
    }            
}

?>

Now I can use this method in every controller and this is a template for my design views!
#6

[eluser]maltzurra[/eluser]
No problem Smile
#7

[eluser]WebbHelp[/eluser]
ehm... Can I mark this thread as solved? And maybe give you a little star Tongue




Theme © iAndrew 2016 - Forum software by © MyBB