未设置的变量会导致致命错误?

时间:2011-01-07 18:59:42

标签: php unset

我不知道为什么这会引发错误 - 希望有人能看到这个问题?

$client->set('place', 'home');
$client->placeInfo();
$client->screen($client->response());
$client->unset('place');
  

致命错误:调用未定义的方法   客户端未设置::()...

客户端类

// stores the client data
private $data = array();
// stores the result of the last method
private $response = NULL;

/**
* Sets data into the client data array. This will be later referenced by
* other methods in this object to extract data required.
*
* @param str $key - The data parameter
* @param str $value - The data value
* @return $this - The current (self) object
*/
public function set($key, $value) {
$this->data[$key] = $value;
return $this;
}

/**
* dels data in the client data array.
*
* @param str $key - The data parameter
* @return $this - The current (self) object
*/
public function del($key) {
unset($this->data[$key]);
return $this;
}

/**
* Gets data from the client data array.
*
* @param str $key - The data parameter
* @return str $value - The data value
*/
private function get($key) {
return $this->data[$key];
}

/**
* Returns the result / server response from the last method
*
* @return $this->response
*/
public function response() {
return $this->response;
}


public function placeInfo() {
$this->response = $this->srv()->placeInfo(array('place' => $this->get('place')));
return $this;
}

1 个答案:

答案 0 :(得分:6)

问题的原因是该类没有定义的方法unset。而且您无法定义一个,因为unsetreserved keyword。您无法使用它的名称来定义方法。

public function unset($foo) // Fatal Error

public function _unset($foo) // Works fine

将方法重命名为其他内容,然后更改呼叫...

修改:查看刚编辑的代码,您需要将$client->unset('place')更改为$client->del('place'),因为这是类定义中的方法... < / p>