PHP 停止构造函数的最佳方法


PHP Best way to stop constructor

我正在处理停止构造函数。

public function __construct()
{
   $q = explode("?",$_SERVER['REQUEST_URI']);
   $this->page = $q[0];
   if (isset($q[1]))
      $this->querystring = '?'.$q[1];
   if ($this->page=='/login') {include_once($_SERVER['DOCUMENT_ROOT'].'/pages/login.php');
      // I WANT TO EXIT CONSTRUCTOR HERE
}

有停止/退出构造函数的功能:

die() , exit(), break()返回 false

我正在使用返回假,但我对安全性感到困惑。退出构造函数的最佳方法是什么?

谢谢你的时间。

一个完整的例子,因为问题应该有一个公认的答案:

在构造函数中引发异常,如下所示:

class SomeObject {
    public function __construct( $allIsGoingWrong ) {
      if( $allIsGoingWrong ) {
        throw new Exception( "Oh no, all is going wrong! Abort!" );
      }
    }
}

然后,在创建对象时,捕获如下错误:

try {
  $object = new SomeObject(true);
  // if you get here, all is fine and you can use $object
}
catch( Exception $e ) {
  // if you get here, something went terribly wrong.
  // also, $object is undefined because the object was not created
}

如果由于某种原因您在任何地方都没有捕获错误,则会导致致命异常,这将使整个页面崩溃,这将解释您"未能捕获异常"并向您显示消息。