• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
setting variables in controller confusion

#1
[eluser]september28[/eluser]
Hi there,

Just wondering if i have 2 functions in a controller say add and add2, could i set a variable in add and then reuse it in add2??

eg.
Code:
function add() {
    $this->myvariable = 'hello';
}

function add2() {
    echo $this->myvariable;
}

In my particular example each function is a different stage of a form and i only want to have to define the id for a record once and then reuse it at various stages of the form (add, add2 and add2)

Thnaks in advance

#2
[eluser]TheFuzzy0ne[/eluser]
Yes. That's the beauty of PHP objects!

#3
[eluser]september28[/eluser]
That's what I thought but it doesn't seem to work...

I have a form in the view that corresponds to the add function. In this view, the form submits to the url /link/add2 (link is the controller name). When I try to use $this->myvariable in add2 function it doesn't recognise the $this->myvariable....

Is it because there are 2 browser calls to the class so the object is created separately twice?
Or do I need to declare the variable in the constructor function? E.g. var $myvariable

Cheers

#4
[eluser]Dam1an[/eluser]
You're right, the variables get reset because each function in the controller (page) is a seperate browser request.
if you need to maintain this information accross pages, you could either set it in the constructor, or use sessions


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.