Welcome Guest, Not a member yet? Register   Sign In
Assigning variables to whole controller [Best Practice?]
#1

[eluser]Unknown[/eluser]
I have a class that has multiple functions in it. There are some variables that I want to be referenced upon through-out the whole controller class.

I have the following variables
Code:
$affid = $this->session->userdata('affid');
$startdate = preg_replace('/[^\d-]+/', '', $this->input->get_post('start'));
$enddate = preg_replace('/[^\d-]+/', '',$this->input->get_post('end'));
if(!$startdate)
{
$startdate = date("Y-m-d");
}
if(!$enddate)
{
$enddate = date("Y-m-d");
}
$getsort = htmlspecialchars($this->input->get_post('sort'),ENT_QUOTES);        
$direction = htmlspecialchars($this->input->get_post('dir'),ENT_QUOTES);
if($getsort && $direction)
{
$sort[$getsort] = $direction;
}

I want these variables with these values to be able to be called upon in other functions
inside my controller class.

I tried putting the above code in my constructor, but the variables cannot be referenced in any other
functions within the class besides the constructor.

Any suggestions on how I can make variables global to my class?
#2

[eluser]techgnome[/eluser]
Declare them publically in the class block...

Code:
class SomeClass extens CI_Example
{
  public $someVar;
  public $someOtherVar;

  function __construct()
  {
    $this->someVar = "This is some var!";
  }

  function somethingelse()
  {
    echo $this->someVar;
  }

} // End Class

Does that help?

-tg
#3

[eluser]Bartolo![/eluser]
It did for me... ;-)

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB