2012-07-12 10 views
1

PHP 예외를 형 변환하고 싶습니다. 다음 코드를 고려하십시오PHP 타입 변환 생성자

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에 표준 예외를 설정하는 것입니다. 추적을 포함하는 변수가 private이기 때문에이 값을 즉시 복사 할 수 없으며 CTOR는 myException에 대해 새로운 값을 생성합니다.

첫 번째 아이디어는 당연히 복제품을 사용하는 것이었지만 $를 다시 할당 할 수는 없었습니다.

그래서 내가 뭘 하려는지 C++ 스타일 형 변환 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); 
    } 
    } 

편집 : 나는 멀리이 코드 이전/w 가지고 왜

가에 대한 아래의 주석을 참조하십시오.

왜 데코레이터로 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(); 
    } 
} 

학습과 정보 :

내가 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'입니다. –

+0

충분히 이상하게도 위의 코드는 w/o 오류로 실행되거나 'error_reporting = E_ALL | E_STRICT', 작동하는지 확인하십시오. – quickshiftin

+0

좋은 주인, PHP는 단순히 개인 값을 하위 클래스에서 개인 메서드를 호출 할 때와 같은 방식으로 설정하려고하면 불평하지 않는다 ... 다른 생각이 있습니다. 나는 최소로 게시 할 것이다. – quickshiftin