Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Controller:

function act() {
    //some code for connection
    $input = (response from client);
    return $input;
}

This is the first time the act will be called so as to connect to the client. Here I'll get the input variable with connection.

function a() {
    $a = $this->act();  
}

How would i get the $input in this function without making the connections again?

function b() {

}

I have tried putting it the session flashdata but it's not working.

share|improve this question
 
why dont you just do the same thing with what you did in function a() ? –  Þaw Jun 7 at 9:47
 
Because that would presumably make the connections again, which he said he wants to avoid? –  Pudge601 Jun 7 at 9:49
 
then that means he needs to pass the $input through parameters –  Þaw Jun 7 at 9:55
 
can you elaborate please.. pass it as parameter in the sense in the fuction call. –  user2462648 Jun 7 at 10:22
add comment

4 Answers

up vote 0 down vote accepted

Its simple in your class define a variable like

 in controller class below function is written. 
Class myclass {
public  $_customvariable;

function act(){
   //some code for connection
 $this->_customvariable=  $input = (response from client);
   return $input;
}

function a() {
$a = $this->act();  
}
function b(){
 echo $this->_customvariable;//contains the $input value 
    }

 }
share|improve this answer
 
thank you its working.. –  user2462648 Jun 7 at 10:19
 
you are welcome :) –  M Khalid Junaid Jun 7 at 10:26
add comment

You can't.

In order to get to that variable, you'd need to put it outside of the function itself.

class MyController extends CI_Controller
{
    private $variable;

    private function act()
    {
        $input = (response from client)
        return $input
    }

    private function a()
    {
        $this->variable = $this->act();
    }
}

Doing that will make you able to access the variable from everywhere within the class.
Hope this helps.

share|improve this answer
add comment
class fooBar {

    private $connection;

    public function __construct() {
        $this->act();
    }

    public function act(){
       //some code for connection
       $this->connection = (response from client);
    }

    public function a() {
        doSomething($this->connection);
    }

    public function b() {
        doSomething($this->connection);
    }
}
share|improve this answer
add comment

You can use statics variables inside methods or functions, the response is kind of "cached" in the function

function act(){
    static $input;
    if (empty($input))
    {
        //some code for connection
        $input = (response from client);
    }
    return $input;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.