6

I have a controller that has the next 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 
    }

}

I thanks for your answers. How can I pass variables of a function to other function in a controller of codeigniter?

Colin Brock
  • 21,267
  • 9
  • 46
  • 61
cabita
  • 832
  • 2
  • 12
  • 36
  • Also, depending on whether or not `myotherfunction` should be callable through a url, you can name it with an underscore so it won't be available by default, such as `private function _myotherfunction(){}`. – Matthew Nov 20 '11 at 16:29

2 Answers2

5

Or you can set $variable as an attribute in you 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 to all functions/methods in your controller class. Think OOP not procedural.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Rooneyl
  • 7,802
  • 6
  • 52
  • 81
  • it's a great idea, however, when in mymotherfunction(), i am going to print the variable, don't show the value, the page is in white. localhost/myproject/controller/myotherfunction (don't print echo $this->variable), will be to load some in the constructor? thanks. I'm sorry my english. – cabita Nov 21 '11 at 14:36
  • sorry, can't understand. If you just want to print the variable(why are you doing this from a controller?), just do echo $this->variable; Or are you getting the infamous Codeigniter white screen of death? – Rooneyl Nov 21 '11 at 15:19
  • @cabita If you want to send me your code, I'll take a look at it and send it back (hopefully working) – Rooneyl Nov 21 '11 at 15:22
  • echo $this->variable don't print. Here my code: variable = 'hola'; } function myfn2() { $variable2= $this->variable; echo $variable2; echo $this->variable; } } ?> Thanks for your help. – cabita Nov 21 '11 at 16:10
  • I have edited my main answer to show you the code. I would recommend having a look at how OOP works. Also this would work the same for your models. – Rooneyl Nov 22 '11 at 09:09
  • Thanks for your answer and your clarification. I'll review the concepts about OOP. – cabita Nov 22 '11 at 09:41
4

You need to define a parameter formyOtherFunction and then simply pass the value from myFunction():

function myFunction(){
    $variable = 'hello';
    $this->myOtherFunction($variable);
}

function myOtherFunction($variable){
    // $variable passed from myFunction() is equal to 'hello';
}
Colin Brock
  • 21,267
  • 9
  • 46
  • 61
  • Hello. I put the next code: function myfn1() { $variable = 'hola'; $this->myfn2($variable); } function myfn2($variable) { echo $variable; } and I appears the next error: A PHP Error was encountered Severity: Warning Message: Missing argument 1 for testdropdown::myfn2() Filename: controllers/testdropdown.php Line Number: 20 A PHP Error was encountered Severity: Notice Message: Undefined variable: variable Filename: controllers/testdropdown.php Line Number: 22 Thanks for your help. – cabita Nov 21 '11 at 01:07