2012-07-12 72 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