简体   繁体   English

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

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

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? 我是C#程序员,我的逻辑表明无论IF如何它都会运行,对吗? Thanks a million. 太感谢了。

When you throw an exception, that usually causes the remaining code to exit unless you have a try...catch statement. 引发异常时,通常会导致其余代码退出,除非您有try...catch语句。 Thus, if the amount is greater than 200 and the user is not authorized, it will execute the block inside of the if statement. 因此,如果该数量大于200并且用户未被授权,它将在if语句内部执行该块。

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 if will not be called. 它不会返回您的模型,因此if后面的行将不会被调用。

Check out the PHP manual on exceptions for more information. 有关异常的更多信息,请查阅PHP手册

异常将中断代码执行。

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

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

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