Welcome Guest, Not a member yet? Register   Sign In
Global variables in controllers
#1

[eluser]dpgtfc[/eluser]
Hopefully I can explain this well, but it seems the controllers don't act as a regular class. I can't create a protected variable within the class and make setters and getters.

The problem I have is the repetition of code. For example I have a controller named Home, of which has several functions for each page, "home", "about", "contact" etc. I'm also using the template class, though that doesn't matter too much I don't think.

Anyway, on some of these I have several repeating variables, such as $data['content'] but some that change, such as $data['title']. I tried instantiating the array in the constructor, but that doesn't work. So like regular OO Php, I tried making a protected (switched to just var) outside of the constructor and tried using this:

Code:
class Home extends Controller{
    
    var $arr;

    function Home()
    {
        parent::Controller();
        $tmp = array();
        $this->load->library('parser');
        $this-arr = array(
        "content"=>""content" => $this->parser->parse('pages/conte/home_content_view',$tmp, TRUE)");
        

    }
    function index(){
    ... etc


Right now I have about 6 variables stored in the array, and on about half of the pages, 3-4 of the variable are the same. Some variables in the template I have blank, such as the admin menu which will only show up if you are logged in as admin. So I'd like to instantiate those variables as blank, such as $data['admin'] = "";

So how do I go about doing this, I've made my own classes before and I was always able to do things like $this->arr = $myArray; but I get "unexpected =" errors.

Is it possible to create an assoc array that is used by all functions in a particular controller class? Or do I have to repeat my code in each function? This makes making any changes a bit tedious, as I have to change every function and doesn't seem very scalable. This is what OOP is supposed to take care of, right?


Messages In This Thread
Global variables in controllers - by El Forum - 09-11-2008, 08:00 AM
Global variables in controllers - by El Forum - 09-11-2008, 09:39 AM
Global variables in controllers - by El Forum - 09-11-2008, 09:40 AM
Global variables in controllers - by El Forum - 09-11-2008, 10:06 AM
Global variables in controllers - by El Forum - 09-11-2008, 10:44 AM
Global variables in controllers - by El Forum - 10-04-2008, 03:47 AM
Global variables in controllers - by El Forum - 10-04-2008, 07:59 AM
Global variables in controllers - by El Forum - 10-04-2008, 08:35 AM
Global variables in controllers - by El Forum - 10-04-2008, 09:32 AM



Theme © iAndrew 2016 - Forum software by © MyBB