コントローラ内のメソッドで変数を定義しました。
class test extends CI_Controller {
function tester() {
$variable = 'value'
}
}
ここで、モデルでこの変数を呼び出します。そんなことがあるものか?
編集:私はCodeIgniterを使用しています。
コントローラ内のメソッドで変数を定義しました。
class test extends CI_Controller {
function tester() {
$variable = 'value'
}
}
ここで、モデルでこの変数を呼び出します。そんなことがあるものか?
編集:私はCodeIgniterを使用しています。
モデル:
class your_model extends CI_Model {
var $variable;
function __construct() {
parent::__construct();
}
function set_variable($variable) {
$this->variable = $variable;
}
}
コントローラ:
class test extends CI_Controller {
function tester() {
$this->load->model('your_model');
$variable = 'value'
$this->your_model->set_variable($variable);
}
}
参考までに、コントローラーやモデルで複数のメソッドに使用できる変数が必要な場合は、クラスの「コンストラクター」に設定できます。変数名の前に$this->を使用します。
class Test extends CI_Controller {
public function __construct() {
parent::__construct();
// Set var in construct
$this->variable123 = 'some value 123' ;
} // end construct
これで、クラスのどこからでも$ this-> variable123を呼び出すことができ、使用できるようになります。このクラスからモデルをロードすると、モデル内のすべてのメソッドで使用できるようになります。