Welcome Guest, Not a member yet? Register   Sign In
Defining variables to be used throughout a controller
#1

[eluser]Fielder[/eluser]
I have a variable say $var that is used throughout the entire controller. How do I define it at the beginning of the controller so I can call it anywhere in any function in the controller?
Is this correct?

Code:
<?php

class ContractStore_Form extends Controller
{
    
    function ContractStore_Form()
    {
        parent::Controller();

        $this->load->library('DX_Auth');
        $this->load->library('RTUI_App');
        $this->load->library('RTUI_Form_Elements');
        $this->load->library('XajaxGrid');
        
        $this->dx_auth->check_uri_permissions();
        
        $this->load->model('Stores', 'store', TRUE);
        $this->load->model('Contracts','contract', TRUE);
        
        // Initialize
        $this->_init();
        
    }

    function _init()
    {
        $this->var = 2;
    }
    function somefunc($storename_name)
    {
        $newvalue = $this->var + 4;

        return $newvalue;
    }
    function anotherfunc($blah)
    {
        $newvalue = $this->var + 8;

        return $newvalue;
    }
.
.
.

And second to that, what if I wanted a function to perform a task to create a variable, but I didn't want to return it. I just wanted the results of the function to be available anywhere else in the controller.

Code:
function open()
{
$this->rowopen = "<tr><td>";
}

function close()
{
$this->rowclose = "</td></tr>";
}

function render()
{
$final = $this->rowopen . $this->rowclose;
}

I've tried just this and it doesn't work... I suspect something having to do with how the controller is setup.?
Thanks.
#2

[eluser]Thorpe Obazee[/eluser]
Code:
class ContractStore_Form extends Controller{
var $var = 2
#3

[eluser]JoostV[/eluser]
And call it in methods using
Code:
// Change var
$this->var = 3;
#4

[eluser]Colin Williams[/eluser]
And you are doing
Code:
$final = $this->rowopen . $this->rowclose;
but you should be doing
Code:
$final = $this->open() . $this->close();
#5

[eluser]tonanbarbarian[/eluser]
[quote author="Colin Williams" date="1246530677"]
Code:
$final = $this->open() . $this->close();
[/quote]
This will not work because the open and close methods are not returning anything, however open and close must both be called before you can access their values

Code:
$this->open();
$this->close();
...
$final = $this->rowopen.$this->rowclose;
and of course you might want to do something with $final such as echo etc
#6

[eluser]Colin Williams[/eluser]
Bah!... Thanks for righting my wrong, tonanbarbarian
#7

[eluser]xwero[/eluser]
for the rowopen, rowclose and i guess cellcloseopen and emptyrow why not use globals as you are going to use then on each page where a form is. you can add those to the config/constants.php file.




Theme © iAndrew 2016 - Forum software by © MyBB