Welcome Guest, Not a member yet? Register   Sign In
where to put vars for use in all controller's functions?
#1

[eluser]alpineedge3[/eluser]
Hello, I think my question has to do with scope in OO structure.

I have the following controller:

Code:
include("myglobals.php");

class Photo extends Myglobals {
        
    function index()
    {
        $this->load->model('m_photo','',TRUE); // true connects to db.
        $data = array();
        $data += $this->globalArray;
        $data['heading'] = 'photo';
        $data['title'] .= $data['heading'];
        $data['subhead'] = 'pictures from my travels';

        $data['albums'] = $this->m_photo->getAlbums();
        $this->load->view('v_photoalbums',$data);
            
    }
    
}

I want to add other functions and want the $data vars to be available to them as well. So should I move them to a constructor? Can you give me a short example of how the vars would be defined within the constructor? And then, how do I access the variables within the functions? do I use $this-> syntax? i'm new to class structures.

thanks
#2

[eluser]Grahack[/eluser]
Code:
<?php
class Photo extends Myglobals {

var $my_var = '';

    function Photo()
    {
        parent::Controller();        // don't forget this !!!
        $this->my_var = 'initialized';
    }

    function index()
    {
        echo $this->my_var;
    }  
}

should work.
#3

[eluser]Craig A Rodway[/eluser]
EDIT: oops, reliased I'd put exactly what previous poster did :red: Wink
#4

[eluser]alpineedge3[/eluser]
thanks Grahack, that worked mostly. one issue is that I need $data to take all the global variables (in $this->globalArray) and add them to the $data array so I can pass $data to views. I have this:

Code:
class Photo extends Myglobals {
    
    var $data;
    
    function Photo(){
        parent::Controller();
        $this->data = array();
        $this->data = $this->globalArray;
        $this->data['heading'] = 'photo';
    }

but i can't access the globalArray as a data variable in the views.
#5

[eluser]Grahack[/eluser]
What is your code when you load the view? It should be something like:
Code:
$this->load->view('view_file', $this->data);
#6

[eluser]alpineedge3[/eluser]
yes that's it.

Code:
$this->load->view('v_photoalbums',$this->data);

I have global variables from Myglobals class that I also want to pass to the view so I thought I would add them to $this->data. Is there a better way?
#7

[eluser]Grahack[/eluser]
Code:
$mega_array_of_the_death = array_merge( $this->data, $this->globalArray );
$this->load->view('v_photoalbums',$mega_array_of_the_death);
#8

[eluser]alpineedge3[/eluser]
ha even the mega array of death cannot squash my problems. When I array_merge in the constructor, I get an error that argument #2 (i.e. $this->globalArray) is not an array. Here's my global class:

Code:
class Myglobals extends Controller {

    // This class sets up static variables throughout the site.
    // It also extends the Controller class, required for all controllers.
    
    var $globalArray;
    
    function Myglobals(){
        parent::Controller();
        $this->load->helper('url');
        $this->globalArray =
                        array(
                                'title' => 'marzloff media - ',
                                'sections' => array('home','news','photo','art','music','cv','links')
                                            
                        );
    }
        
}

so why doesn't it think it's an array?
#9

[eluser]Grahack[/eluser]
I think it's a constructor problem. I'm not a OOP guru in php, sorry. I don't know how to fix your problem in one shot. Please paste your construction functions only.
Try a die('I was here') in function Myglobals() just to be sure it's executed before the loading of the view.
#10

[eluser]alpineedge3[/eluser]
ok well thanks for your help so far. and the die() works.

update:
i finally solved the problem- I was using parent::Controller instead of parent::Myglobals in the constructor. grr!




Theme © iAndrew 2016 - Forum software by © MyBB