繁体   English   中英

如果IF条件为假,为什么要执行这一行代码? 没有任何意义

[英]Why would this line of code be executed if the IF condition is false? Doesn't make any sense

这是代码:

<?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);
 }
 }

如果条件失败,为什么运行该方法。 我是C#程序员,我的逻辑表明无论IF如何它都会运行,对吗? 太感谢了。

引发异常时,通常会导致其余代码退出,除非您有try...catch语句。 因此,如果该数量大于200并且用户未被授权,它将在if语句内部执行该块。

您提供的链接提到它将“冒泡”到被捕获的控制器。 由于未在上面的代码(模型)中捕获该代码,因此该模型内部的执行停止,并向上传递给控制器​​。 它不会返回您的模型,因此if后面的行将不会被调用。

有关异常的更多信息,请查阅PHP手册

异常将中断代码执行。

即使在C#中,即使抛出异常,也不会执行代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM