Here’s the code:
<?php
class Order extends Zend_Db_Table_Abstract
{
protected $_name = 'orders';
protected $_limit = 200;
protected $_authorised = false;
public function setLimit($limit)
{
$this->_limit = $limit;
}
public function setAuthorised($auth)
{
$this->_authorised = (bool) $auth;
}
public function insert(array $data)
{
if ($data['amount'] > $this->_limit && $this->_authorised === false) {
throw new Exception('Unauthorised transaction of greater than ' . this->_limit . ' units');
}
return parent::insert($data);
}
}
Why would that method run ONLY if the condition fails. I’m a C# programmer, I my logic dictates that it will run regardless of the IF, correct? Thanks a million.
When you throw an exception, that usually causes the remaining code to exit unless you have a
try...catchstatement. Thus, if the amount is greater than 200 and the user is not authorized, it will execute the block inside of theifstatement.The link you provided mentions that it will “bubble up” to a controller where it will be caught. Since it’s not caught in your code above (the model), execution inside of the model stops and is passed up the stack to the controller. It does not return to your model, thus the line following the
ifwill not be called.Check out the PHP manual on exceptions for more information.