variables between display() and definition() functions

variables between display() and definition() functions

by Victor Martin -
Number of replies: 4

Hi again, I have my php file as follows:

class khronos_punching_form extends moodleform {
...

function display () {
$endtime = ...;

...

}

function definition () {

// I need $endtime here

...
}

...
}

My question is how I can take a variable from display frunction for use in definition function. With global variables I can not get the value of any variable.
How should I do it?

 

Thanks

Average of ratings: -
In reply to Victor Martin

Re: variables between display() and definition() functions

by Shane Elliott -
Picture of Core developers Picture of Plugin developers

Perhaps define a class property eg:

class khronos_punching_form extends moodleform {
  var $endtime;
...
function display () {
   $this->endtime = ...;
...
}
function definition () {
   echo $this->endtime;
...
}
}

You could also pass it into the form instance as the second parameter and reference it using $this->_customdata eg
$myform = new khronos_punching_form(null, $endtime);

Hope that helps

Cheers,
Shane.

In reply to Shane Elliott

Re: variables between display() and definition() functions

by Victor Martin -

I'm trying to use like you say. I declare a var $endtime, in display I do $this->endtime = something, and later in definition I do for example, echo $this->endtime; I don't obtain any value.

In reply to Victor Martin

Re: variables between display() and definition() functions

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe I'm missing something here, but isn't the definition() function called before the display() function? (You can't display a form you haven't defined!)

In which case, setting the variable in the display function after you have tried to use it in the definition function is never going to work.