在重构的PHP代码中停止执行?

在重构的PHP代码中停止执行?

问题描述:

I have just refactored a php page to make it slightly easier to extend and maintain in the future and gotten stuck on quite a simple problem.

I have broken one method up into 3.

largemethod() has become something like this:

nowsmallmethod(){
  doSomeChecks();
  assignProduct();
  giveFeedbacktoUser();
}

This is all good and well, the problem I am having is with doSomeChecks();

doSomeChecks(){
 if(something that shouldnt be true is true){
    return Controller->redirectBk();
 }
}

The crux of the problem is that Controller-redirectBk first redirects when nowsmallmethod() has been completed. This means that the user is assigned a product even if a test fails. I am using a php framework called Silverstripe so I cant really change the behavior of Controller->redirectBk(). If I didnt have the checks in their own method then everything would work fine because the "return Controller->redirectBk();" would stop execution and redirect back. Whats the best way to stop execution in nowsmallmethod() when a test fails? I realise i could return a status code for an error and then stop execution but it seems an ugly way. Is there not a more elegant way? Another option would be if i could return something like this in doSomeChecks(), "return (return $controller->redirectBk())" but this is not valid php syntax and not particularly easy to read. Any help would be hugely appreciated.

Enjoy your weekend! Cheers Nick

我刚刚重构了一个php页面,以便将来稍微更容易扩展和维护,并且相当坚持 一个简单的问题。 p>

我已经将一个方法分解为3个。 p>

largemethod()已经变成这样: p>

  nowsmallmethod(){
 doSomeChecks(); 
 assignProduct(); 
 giveFeedbacktoUser(); 
} 
  code>  pre> 
 
 

这一切都很好,我遇到的问题是doSomeChecks(); p>

  doSomeChecks(){
 if(不应该为真的事情){\  n return Controller-> redirectBk(); 
} 
} 
  code>  pre> 
 
 

问题的症结在于,当nowsmallmethod()拥有时,Controller-redirectBk首先重定向 已经完成。 这意味着即使测试失败,也会为用户分配产品。 我正在使用一个名为Silverstripe的php框架,因此我无法真正改变Controller-> redirectBk()的行为。 如果我没有用他们自己的方法进行检查,那么一切都会正常工作,因为“return Controller-> redirectBk();” 会停止执行并重定向回来。 当测试失败时,什么是在nowsmallmethod()中停止执行的最佳方法? strong>我意识到我可以返回错误的状态代码,然后停止执行,但这似乎是一种丑陋的方式。 有没有更优雅的方式? 另一种选择是如果我可以在doSomeChecks()中返回这样的东西,“返回(返回$ controller-> redirectBk())”但这不是有效的PHP语法,也不是特别容易阅读。 任何帮助都将非常感激。 p>

享受你的周末! 欢呼 Nick p> div>

nowsmallmethod() {
  if (doSomeChecks()) {
    assignProduct();
    giveFeedbacktoUser();
  }
}

And doSomeChecks either returns true or false, based on whether the redirect will happen or not.

Alternatively, you could die or throw, but I assume a normal condition is more suitable in your case.

Use exceptions to handle situations like that:

<?php
try {
    // some code
    if (!$check) {
         throw new Exception('Check has failed!');
    }
    // code after 'throw' will not be executed
} 
catch(Exception $e) {
    //this is executed when exception is caught
}
?>

If the exception is thrown in child method, the stack will roll back to the very first try/catch block. read more about the exceptions: http://php.net/manual/en/language.exceptions.php

Also, you can combine exceptions with transaction mechanism to handle database integrity.