Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter global variable within controller

Tags:

php

I have a controller with a couple of methods and they all have the same variable within them. I am looking for a way to have them share the same variable. What is the best way to do this?

What I have now:

class Example extends CI_Controller{
  public function test{
    $variable = "awesome";
  }

  public function demo{
    $variable = "awesome";
  }

}
like image 888
ThomasReggi Avatar asked Aug 22 '11 18:08

ThomasReggi


People also ask

How declare variable in PHP CodeIgniter?

In CodeIgniter 4, we have a folder like app/config/Constant. php so you can define a global variable inside the Constant. php file.


1 Answers

How about...

class Example extends CI_Controller
{

  public $variable = "awesome";

  public function test()
  {
    echo $this->variable; // awesome
  }

  public function demo()
  {
    echo $this->variable; // awesome
  }

}
like image 86
65Fbef05 Avatar answered Oct 03 '22 16:10

65Fbef05