2

コントローラ:

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

クライアントに接続するためにアクトが呼び出されるのはこれが初めてです。ここでは、接続を使用して入力変数を取得します。

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

$input接続を再度作成せずに、この関数でを取得するにはどうすればよいですか?

function b() {

}

セッションのフラッシュデータを入れてみましたが、うまくいきません。

4

4 に答える 4

3

できません。

その変数に到達するには、関数自体の外に置く必要があります。

class MyController extends CI_Controller
{
    private $variable;

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

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

これにより、クラス内のどこからでも変数にアクセスできるようになります。
お役に立てれば。

于 2013-06-07T09:53:31.593 に答える
2

class次のような変数を定義するのは簡単です

 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 
    }

 }
于 2013-06-07T09:49:26.350 に答える
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);
    }
}
于 2013-06-07T09:50:53.927 に答える
0

メソッドまたは関数内で静的変数を使用できます。応答は関数内で「キャッシュ」されます。

function act(){
    static $input;
    if (empty($input))
    {
        //some code for connection
        $input = (response from client);
    }
    return $input;
}
于 2013-06-07T09:53:11.807 に答える