Pass variables to another function in the controller in the encoder?

I have a controller that has the following functions:

class controller { function __construct(){ } function myfunction(){ //here is my variable $variable="hello" } function myotherfunction(){ //in this function I need to get the value $variable $variable2=$variable } } 

Thank you for your responses. How to pass function variables to another function in codeigniter controller?

+6
source share
2 answers

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.

+5
source

You need to define a parameter for myOtherFunction , and then just pass the value from myFunction() :

 function myFunction(){ $variable = 'hello'; $this->myOtherFunction($variable); } function myOtherFunction($variable){ // $variable passed from myFunction() is equal to 'hello'; } 
+4
source

Source: https://habr.com/ru/post/901947/


All Articles