簡體   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