【发布时间】:2011-12-30 04:23:37
【问题描述】:
我有一个函数允许访问我以前从未见过的变量函数。
正常功能:
$api = api_client($special_data);
$data = $api('get','something.json'); // notice $api() not a mistake
上面这个例子的问题是我在我的控制器的每个函数/方法中创建了 $api 变量。我想做这样的事情:
public $api;
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
}
public function anotherpage(){
$data = $this->api('get','something.json'); // api is not a function it is a variable function
}
我确实发现以下工作,虽然我不满意
public function somepage(){
$special_data = get_special_data_from_this_method();
$this->api = api_client($special_data);
$temp = $this->api;
$data = $temp('GET', '/admin/orders.json');
}
希望这有意义会喜欢帮助!
【问题讨论】:
-
你试过了吗?有用吗?
-
是的,我试过了,不,它不起作用
$this->api()被认为是一个函数,错误是Call to undefined method mycontroller::api() -
你能把它变成静态的吗?
public static $api;然后调用self::$api('get','something');还是需要特定于实例? -
即使浏览了stackoverflow.com/questions/151969/php-self-vs-this,也不热衷于差异,但我试过了,它也不起作用
Undefined variable: api和Function name must be a string in ... -
静态属性和方法由类的所有实例共享。每个对象都没有自己的,所以如果一个对象发生变化,它会反映在所有类实例(对象)中。我忘记了上面的一部分。我会把它作为答案。
标签: php model-view-controller class codeigniter