2

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.

4
  • why dont you just do the same thing with what you did in function a() ? Commented Jun 7, 2013 at 9:47
  • Because that would presumably make the connections again, which he said he wants to avoid? Commented Jun 7, 2013 at 9:49
  • then that means he needs to pass the $input through parameters Commented Jun 7, 2013 at 9:55
  • can you elaborate please.. pass it as parameter in the sense in the fuction call. Commented Jun 7, 2013 at 10:22

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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 
    }

 }

Comments

0
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);
    }
}

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.