Welcome Guest, Not a member yet? Register   Sign In
extending the controller class
#1

[eluser]Doodlez[/eluser]
I have a certain number of elements that are going to be called on my page over and over again. I planned on extending the controller class with functions that generate the elements.

For example my header is static with a few small changes....


Code:
<?php
class Legos extends Controller{
function Legos(){
        parent::Controller();
        }
function head(){

        $this->load->helper('url');
        $this->load->helper('html');
                    $header['navLink']=array(anchor('main','Home'),anchor('events','Events'),anchor('contact','Contact'),anchor('submissions','Submissions'));

                        $decleration['title']='jagazineDream';
                        $decleration['meta']=array('name'=>'robots','content'=>'taking over the world');

                                $this->load->view('decleration',$decleration);
                                $this->load->view('header',$header);          
                        }
}

?>

and then any time I needed my header I can call it ............

Code:
<?php
class Main extends Legos
{
        function index()
{
        parent::head();
        }

}
?>

my plan is to just create the other elements of my page as functions and just keep extending the controller class. I have never really done any object based programming I kind of guessed my way. Is there any where I will get my self in trouble creating a site like this?
#2

[eluser]jedd[/eluser]
Hi Doodlez,

[quote author="Doodlez" date="1273244440"]
I planned on extending the controller class with functions that generate the elements.
[/quote]

You might want to get comfortable with using the built-in way of extending the core controller class - read about it [url="/wiki/MY_Controller"]in the wiki[/url].
#3

[eluser]Doodlez[/eluser]
Ok I can now call my functions as $this and don't have to call them using parent. It is late and it may be obvious but why the way I did it originally did I need to call the method as the parent and not class.

It may be naive but the way I did it and your tutorial suggest appear to be the same thing. Is it the way that CI uses the MY_ namespace internally or something like that?

I know I may not be wording this question well.
#4

[eluser]jedd[/eluser]
[quote author="Doodlez" date="1273420871"]
It may be naive but the way I did it and your tutorial suggest appear to be the same thing. Is it the way that CI uses the MY_ namespace internally or something like that?
[/quote]

Yes - it's configurable, and defaults to MY_ - have a read about it in the [url="/user_guide/general/creating_libraries.html"]User Guide section on creating libraries[/url].
#5

[eluser]Phil Sturgeon[/eluser]
$this and parent do pretty much the same thing, just one is more specific. When you extend a parent class you pick up all it's properties and methods, so parent::head() and $this->head() are identical. If you have head() in both parent and current THEN it would be different.




Theme © iAndrew 2016 - Forum software by © MyBB