例如,我有一个类似这样的课程
public function data() {
$a = 1;
$b = 2;
$c = 3;
}
public function codeigniter() {
$a, $b, $b //how i can get the value??
}
如何获取$a
,$b
,$c
的值,以便我可以在codeigniter()
函数中使用它?我用codeigniter?我需要添加return;
吗?
答案 0 :(得分:5)
我假设这两个函数同时属于classigniter和codeigniter的控制器类
class Sample extends CI_Controller {
// declare variables
public $a;
public $b;
public $c;
public function __construct() {
// call codeigniter method
$this->codeigniter();
}
public function data() {
$this->a = 10;
$this->b = 11;
$this->c = 12;
}
public function codeigniter() {
// call method data();
$this->data();
echo $this->a; // prints 10
echo $this->b; // prints 11
echo $this->c; // prints 12
}
}
答案 1 :(得分:1)
首先创建一个数组并将值存储在其中并返回数组。
public function data() {
$data = array();
$data['a']=1;
$data['b']=2;
$data['c']=3;
return $data;
}
然后,您只需调用该函数并访问返回的数据。
public function codeigniter() {
$data_value = $this->data();
echo $data_value['a'];
echo $data_value['b'];
echo $data_value['c'];
}
答案 2 :(得分:0)
要从函数中获取多个变量,必须将这些变量放在数组中并返回数组
public function data() {
$a = 1;
$b = 2;
$c = 3;
$data = array();
array_push($data, $a,$b,$c);
return $data;
}//data function ends
public function codeigniter() {
$data = $this->data();
//now use $data
}