开发者

Unset variable causes fatal error?

I have no idea why this is throwing an error - hoping someone can see the issue?

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

Fatal error: Call to undefined method Client::unset()...

client class

// 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开发者_JAVA技巧 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;
}


The cause of your problem is that there is no defined method unset on that class. And you can't define one either because unset is a reserved keyword. You can't define a method with it as it's name.

public function unset($foo) // Fatal Error

public function _unset($foo) // Works fine

Rename the method to something else, and change the call...

Edit: Looking at the code you just edited in, you need to change the $client->unset('place') to $client->del('place'), since that's the method in the class definition...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜