CodeIgniter Forums
For those who thinking of - Class child extends parent/Class extend or inheritance - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: For those who thinking of - Class child extends parent/Class extend or inheritance (/thread-37048.html)



For those who thinking of - Class child extends parent/Class extend or inheritance - El Forum - 12-25-2010

[eluser]Unknown[/eluser]
It's Christmas Break for me, so I decided to take some time to mess with OOP (Object-Oriented Programming) with CodeIgniter. This post is solely dedicated for those who are NEW to CodeIgniter and planning to do any OOP in CodeIgniter.

If you are planning to use OOP in Code Igniter Framework, find another one. But, if you want a framework that is simple and straightforward, Code Igniter is the winner.

I will demonstrate how to use Class extends/inheritance successfully in CI, though it is not the best; Let's take a dive!

Stuff that did not work for me:
METHOD A
Code:
class Love extends Controller{

    function __construct(){
        parent::Controller();
        $this->load->library('table');
        $this->love = "test";
    }
    function index(){

        $this->call();
        $b = new Hate;
        $b->call();
    }

    function call(){
        echo $this->love." PARENT";
    }
}

class Hate extends Love{

    function __construct(){
        parent::__construct();
    }

    function call(){
        $this->load->library('table');
        $data = array(
                     array('Name', 'Color', 'Size'),
                     array('Fred', 'Blue', 'Small'),
                     array('Mary', 'Red', 'Large'),
                     array('John', 'Green', 'Medium')
                     );

        echo $this->table->generate($data);
    }
}
The child class just simply failed as CI 'table' library was not able load. BUT, if you are planning to OOP without utilizing any CI libraries/functions, then you are perfectly OKAY.

*If you want this to work, but can simply cut the "$this->load->library('table');" and paste it to the Child Construct, and make sure the table library is not globally enable. IMO, it's not practical.

*---IF anyone can point out a way to get this method working OR even an explanation pertaining to this scenario, i really do appreciate it Smile

Stuff that worked!! :-
METHOD B
Create love.php in the same folder, which in this case is Controllers.
Code:
class Love extends Controller{
    /**
     * Constructor
     */

    function __construct(){
        parent::Controller();
        $this->load->library('table');
        $this->love = "test";
    }

    function call(){
        echo $this->love." PARENT";
    }
}

Create Hate.php in the same folder as love.php
Code:
require_once(APPPATH.'controllers/love.php');
class Hate extends Love{

    function __construct(){
        parent::__construct();

    }

    function call(){
        $data = array(
                     array('Name', 'Color', 'Size'),
                     array('Fred', 'Blue', 'Small'),
                     array('Mary', 'Red', 'Large'),
                     array('John', 'Green', 'Medium')
                     );

        echo $this->table->generate($data);
        echo $this->love . " 2 hate man";
    }
}

TADA, just call it via index.php/hate/call , it will work like a charm. BUT, this method is sorta NOT the "proper" way of coding for MVC.

THE OFFICIAL/Recommended WAY(IMO, it looks like a workaround to me):
METHOD C
Create a FILE MY_controller.php in application/libraries, EXACT NAME and CAPITAL LETTER, otherwise it will not work. I tried MY_Love, it just doesn't load.
Code:
//Toss all your parent classes here
class MY_Love extends Controller{
    /**
     * Constructor
     */

    function __construct(){
        parent::Controller();
        $this->load->library('table');
        $this->love = "test";
    }

    function call(){
        echo $this->love." PARENT";
    }
}
Then, in your Controllers folder, just create, hate.php
Code:
class Hate extends MY_Love{

    function __construct(){
         parent::__construct();

    }

    function call(){
        $data = array(
                     array('Name', 'Color', 'Size'),
                     array('Fred', 'Blue', 'Small'),
                     array('Mary', 'Red', 'Large'),
                     array('John', 'Green', 'Medium')
                     );

        echo $this->table->generate($data);
        echo $this->love . " 2 hate man";
    }
}

It will work just fine Smile You can check out more on METHOD C on Philsturgeon's site

BUT, my question is why the METHOD A does not work, as METHOD B works?? The only different is that B child class is ran on separate php file. Any comments or explanations will be appreciated!!


For those who thinking of - Class child extends parent/Class extend or inheritance - El Forum - 12-25-2010

[eluser]bl00dshooter[/eluser]
First: Each class is like a controller. You're not suposed to have 2 controllers on the same file, so it doesn't work. There is a reason the filename must be equal to the class name, it's how codeigniter routing works.

Second: Phil Sturgeon already explained how to do what you want in his blog: http://philsturgeon.co.uk/index.php/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY

Third: How's codeigniter not playing well with OOP? It's based on OOP, controllers and models are OOP, etc. I think you incorrectly explained yourself or you don't understand how things work.

Fourth: Merry Christmas, sskl.


For those who thinking of - Class child extends parent/Class extend or inheritance - El Forum - 12-25-2010

[eluser]denver001[/eluser]
<a href="http://motorhomescampervans.net" rel="nofollow">Motorhome Sales</a>