CodeIgniter Forums
How do I subclass one of my own controllers? [SOLVED] - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: How do I subclass one of my own controllers? [SOLVED] (/thread-32424.html)



How do I subclass one of my own controllers? [SOLVED] - El Forum - 07-22-2010

[eluser]CoderReborn[/eluser]
I have a controller named "Done" which extend the Controller class.

I am trying to create a subclass controller called "ToDo" with the following code:
Code:
<?php

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

?&gt;

However, this causes a blank screen.
When I change "Done" to "Controller" (not what I want), the display is correct.

How do I subclass one of my own controllers?

Thanks!


How do I subclass one of my own controllers? [SOLVED] - El Forum - 07-22-2010

[eluser]Jelmer[/eluser]
Maybe just like this? (not pretty but probably works)

Code:
&lt;?php

require APPPATH . 'controllers/done.php';

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

I haven't done this, but I have worked with multiple base controllers. I implemented those by writing an autoloader that would autoload the class after "extends". Thus avoiding needing "require" and it might also be a better solution for you. Write one abstract base controller and then extend both Todo and Done from that base.


How do I subclass one of my own controllers? [SOLVED] - El Forum - 07-22-2010

[eluser]CoderReborn[/eluser]
Thanks, Jeimer! That works.


How do I subclass one of my own controllers? [SOLVED] - El Forum - 01-28-2012

[eluser]jwright[/eluser]
[quote author="Jelmer" date="1279839987"]Maybe just like this? (not pretty but probably works)

Code:
&lt;?php

require APPPATH . 'controllers/done.php';

class Todo extends Done
{

    public function __construct()
    {
        parent::__construct();
        echo "Todo constructor<br/>";
    }

    public function index()
    {
        echo "Inside Todo index <br/>";
    }

}

I haven't done this, but I have worked with multiple base controllers. I implemented those by writing an autoloader that would autoload the class after "extends". Thus avoiding needing "require" and it might also be a better solution for you. Write one abstract base controller and then extend both Todo and Done from that base.[/quote]

I like this for a couple reasons.
1. It explicitly shows the file where the base class is located
2. It should work on any CI install even if an autoloader is not setup

That's not to say autoloaders don't have their place. It's nice to not have to worry about these require statements, especially if you would need a lot of them.