Welcome Guest, Not a member yet? Register   Sign In
passing a variable back to a function
#1

[eluser]Unknown[/eluser]
Hello,

my question is, if I have a function called test1() and this function is called in my controller:

Code:
$this->test1();

but I also wish a function called test2 to pass a variable to it later in the code.

Code:
public function test2(){
   $passed='foo';
   return $passed;
}

$this->test1($passed);

My program throws an error because $passed is not needed the first time. Do I need to override test with another implementation. How would I do this.

Sorry if this is a basic question. Any help or advice would be greatly appreciated.

Thanks

PTVW
#2

[eluser]Kamarg[/eluser]
Your example code doesn't match up with my understanding of what you said you're trying to do. That aside, you can give test1 a parameter with a default value so that it works regardless of if you pass a value to it or not.

Code:
function test1($param = null) {
    // Function code goes here
}
#3

[eluser]Unknown[/eluser]
Thanks for replying, but wouldn't that always set the value to null when its passed too?

Those weren't actual extracts from the application but I didn't illustrate very well. I thought I would need to override.
#4

[eluser]Kamarg[/eluser]
It will only be null if you don't pass a value for the variable. Check the php manual at http://www.php.net/manual/en/functions.a...ts.default for more information.
#5

[eluser]Twisted1919[/eluser]
Code:
class Blah extends CI_Controller(){
  
   public $pass_me_around;

   public function test1(){
      $this->pass_me_around = 'Wow, i can be globally used in this class after i am set, how cool is $this ?';
   }

   public function test2(){
       return $this->pass_me_around ;
   }


}




Theme © iAndrew 2016 - Forum software by © MyBB