berkes berkes - 5 months ago 14
PHP Question

How to throw a Not Implemented Error


Possible Duplicate:

Throw a NotImplementedError in PHP?




When writing base-classes, I often develop gradually, and only build what I need (Or, what I am testing). Yet I like my interface to be balanced. For every getter a setter. When implmenting CRUD, even when right now, I need only the R, I'd rather write the stubs for CUD laready.

I want these to throw some Not Implemented Exception. How can I raise such errors or exceptions?

For example:

class Resource {
/**
* get Issues a GET-request to Online, fetch with $this::$id
* @returns $this
*/
protected function get() {
if ($this->id) {
$attributes = http_build_query(array("id" => $this->id));
$url = "{$this->url}?{$attributes}";
$options = array_merge(array('method' => 'GET'));
$response = _http_request($url, $options);
$this->parse($response);
}
return $this;
}

/**
* post Place a new object on Online
*
* @param $attributes ....
* @returns Source $this
*/
protected function post($attributes = array()) {
throw new NotImplementedError();
}

/**
* put Updates an object on Online
*
* @param $attributes ....
* @returns $this
*/
protected function put($attributes = array()) {
throw new NotImplementedError();
}

/**
* delete Removes an object from Online, selected by $this::$id.
* @returns $this
*/
protected function delete() {
throw new NotImplementedError();
}
}

Answer

Have a look at this previous answer: Throw a NotImplementedError in PHP?

Very simply put, one doesn't exist in PHP, but you can easily create your own by extending an existing Exception. The previous answer suggests using BadMethodCallException to do this:

class NotImplementedException extends BadMethodCallException
{}

Then you can throw a NotImplementedException within your code.

Comments