Or you can set $ variable as an attribute in your class;
class controller extends CI_Controller { public $variable = 'hola'; function __construct(){ } public function myfunction(){ // echo out preset var echo $this->variable; // run other function $this->myotherfunction(); echo $this->variable; } // if this function is called internally only change it to private, not public // so it could be private function myotherfunction() public function myotherfunction(){ // change value of var $this->variable = 'adios'; } }
This way variable will be available for all functions / methods in your controller class. Think that OOP is not procedural.
source share