为什么不以与文件和行相同的方式设置跟踪和先前?
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();
}
}
更多信息:
我已经跟进了 php-general ,结果证明这是预期的行为,它在 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...");
}
}