Welcome Guest, Not a member yet? Register   Sign In
For those who thinking of - Class child extends parent/Class extend or inheritance
#1

[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!!
#2

[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...ing-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.
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB