我有一个带有函数的类,该函数调用变量,并且由于某种原因它在开头设置时显示为空。 (顺便说一下,不是完整的代码)(url用作函数变量中的路径)
public $domain = 'http://rbx-js.herokuapp.com';
protected $key = '';
protected function post($url, $data)
{
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$this->domain.$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}
它将返回如下内容: http:///getPlayers/retrieve/3e75c964ae
答案 0 :(得分:0)
在cosntruct中初始化变量:
public $domain;
protected $key;
protected function __construct()
{
$this->domain = 'http://rbx-js.herokuapp.com';
$this->key = '';
}
protected function post($url, $data)
{
foreach($data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$this->domain.$url);
curl_setopt($ch,CURLOPT_POST,count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
return $result;
}