Reputation: 53
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.
Upvotes: 2
Views: 5387
Reputation: 64466
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
}
}
Upvotes: 2
Reputation: 2395
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.
Upvotes: 3
Reputation: 7010
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;
}
Upvotes: 0
Reputation: 1374
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);
}
}
Upvotes: 0