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

1 php unset

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

$client->set('place', 'home');
$client->placeInfo();
$client->screen($client->response());
$client->unset('place');
Run Code Online (Sandbox Code Playgroud)

致命错误:调用未定义的方法Client :: unset()...

客户类

// 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;
}
Run Code Online (Sandbox Code Playgroud)

irc*_*ell 6

问题的原因是unset该类没有已定义的方法.并且您无法定义一个因为unset保留关键字.您无法使用它的名称定义方法.

public function unset($foo) // Fatal Error

public function _unset($foo) // Works fine
Run Code Online (Sandbox Code Playgroud)

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

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