2012-07-12 95 views
1

我想強調PHP異常。請看下面的代碼:php typecast構造函數

class myException extends Exception { 
    function __construct($mOrigin = "", $iCode = 0, Exception $oPrevious = null){ 
    if(is_string($mOrigin)){ 
     parent::__construct($mOrigin, $iCode, $oPrevious); 
    } elseif ($mOrigin instanceof Exception) { 
     parent::__construct($mOrigin->getMessage(),$mOrigin->getCode(),$mOrigin->getPrevious()); 
     $this->file = $mOrigin->getFile(); 
     $this->line = $mOrigin->getLine(); 
    } else { 
     parent::__construct("\$mOrigin has wrong type", self::eFatal, $oPrevious); 
    } 
    } 

的想法是把一個標準的異常成myException保留堆棧跟蹤。由於包含跟蹤的變量是私有的,我不能立即複製這些值,並且CTOR爲myException生成一個新的值。

第一個想法當然是使用克隆,但我幾乎不能重新分配$ this,對嗎?

所以我想要做的是一個C++風格的typecast CTOR。 PHP中有這樣一個明智的範例嗎?

回答

1

爲什麼不只是設置跟蹤&上一個文件&線?

class myException extends Exception { 
    function __construct($mOrigin = "", $iCode = 0, Exception $oPrevious = null){ 
    if(is_string($mOrigin)){ 
     parent::__construct($mOrigin, $iCode, $oPrevious); 
    } elseif ($mOrigin instanceof Exception) { 
     parent::__construct($mOrigin->getMessage(),$mOrigin->getCode(),$mOrigin->getPrevious()); 
     $this->file  = $mOrigin->getFile(); 
     $this->line  = $mOrigin->getLine(); 
     $this->trace = $mOrigin->getTrace(); 
     $this->previous = $mOrigin->getPrevious(); 
    } else { 
     parent::__construct("\$mOrigin has wrong type", self::eFatal, $oPrevious); 
    } 
    } 

編輯:

看下文的意見,爲什麼我把他瓦特/此代碼前面。

爲什麼不把你的myException類成裝飾:

class myException extends Exception { 
    private $_oException; 

    function __construct($mOrigin = "", $iCode = 0, Exception $oPrevious = null){ 
    if(is_string($mOrigin)){ 
     parent::__construct($mOrigin, $iCode, $oPrevious); 
    } elseif ($mOrigin instanceof Exception) { 
     $this->_oException = $mOrigin; 
     parent::__construct($mOrigin->getMessage(),$mOrigin->getCode(),$mOrigin->getPrevious()); 
     $this->file  = $mOrigin->getFile(); 
     $this->line  = $mOrigin->getLine(); 
    } else { 
     parent::__construct("\$mOrigin has wrong type", self::eFatal, $oPrevious); 
    } 
    } 

    function getTrace() 
    { 
    return $this->_oException->getTrace(); 
    } 

    function getPrevious() 
    { 
    return $this->_oException->getPrevious(); 
    } 
} 

FUTHER信息:

我跟進的PHP一般,它原來這個預期的行爲,並Java等人的作品也是如此。您可以覆蓋子類中的成員變量,並擁有一個具有相同名稱的單獨商店。在java中編譯就好了

public class PrivateAccess 
{ 
    private Boolean isAccessible = true; 

    public Boolean getAccessible() 
    { 
     return isAccessible; 
    } 
} 
class PrivateAccessChild extends PrivateAccess 
{ 
    private Boolean isAccessible = false; 

    public Boolean getAccessible() 
    { 
     return isAccessible; 
    } 

    public Boolean getParentAccessible() 
    { 
     return super.getAccessible(); 
    } 

    public static void main(String[] args) 
    { 
     PrivateAccessChild pAccess = new PrivateAccessChild(); 

     if(!pAccess.getAccessible()) 
      System.out.println("we're hitting the child here..."); 

     if(pAccess.getParentAccessible()) 
      System.out.println("we're hitting the parent here..."); 

     System.out.println("we're done here..."); 
    } 
} 
+0

因爲'trace'和'previous'是** private **到'Exception'。 – 2012-07-12 17:52:43

+0

足夠公平 - 奇怪的是,上面的代碼運行W/O錯誤或通知甚至W /'error_reporting = E_ALL | E_STRICT',看看它是否適合你。 – quickshiftin 2012-07-12 18:08:45

+0

好主,看起來像PHP根本不會抱怨,當你試圖設置一個私人價值的方式,當你從一個子類調用私人方法......,我有另一個想法;我會在一分鐘後發佈。 – quickshiftin 2012-07-12 18:19:05