Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php类型转换构造函数_Php_Oop_Constructor_Clone - Fatal编程技术网

php类型转换构造函数

php类型转换构造函数,php,oop,constructor,clone,Php,Oop,Constructor,Clone,我想输入PHP异常。考虑下面的代码: class myException extends Exception { function __construct( $mOrigin = "", $iCode = 0, Exception $oPrevious = null){ if(is_string($mOrigin)){ parent::__construct($mOrigin, $iCode, $oPrevious); } elseif ($mOrigin ins

我想输入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,保留原始堆栈跟踪。因为保存跟踪的变量是私有的,所以我不能立即复制这些值,而且CTOR会为myException生成一个新的值

第一个想法当然是使用克隆,但我很难重新分配$this,对吗


所以我想做的是C++风格的类型CCTR。PHP中是否有一个合理的范例可以做到这一点?

为什么不将trace&previous设置为与file&line相同的方式

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...");
    }
}

为什么不把trace&previous设置为与file&line相同的方式呢

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...");
    }
}

因为
trace
previous
异常的私有部分
。公平-奇怪的是,上面的代码运行时没有错误,甚至注意到错误报告=E|u ALL | E|u STRICT,看看它是否适合你。天哪,当你试图设置一个私有值时,就像你从一个子类调用一个私有方法时那样,PHP看起来并没有抱怨…,我有另一个想法;我将在一个min.LOL中发布,经过进一步的实验,很明显您甚至可以使用PHP对私有成员变量的愚蠢处理作为一项功能。如果您在myException中为getTrace和getPrevious添加简单的getter方法,那么原始代码就可以工作了。这应该继续有效,即使他们有一天会修正语言。非常感谢。因为
trace
previous
异常的私有部分。非常公平-奇怪的是,上面的代码运行时没有错误,甚至没有注意到错误报告=E|u ALL | E|u STRICT
,看看它是否适用于你。天哪,当你试图设置一个私有值时,就像你从一个子类调用一个私有方法时那样,PHP看起来并没有抱怨…,我有另一个想法;我将在一个min.LOL中发布,经过进一步的实验,很明显您甚至可以使用PHP对私有成员变量的愚蠢处理作为一项功能。如果您在myException中为getTrace和getPrevious添加简单的getter方法,那么原始代码就可以工作了。这应该继续有效,即使他们有一天会修正语言。谢谢。