duanmei2459 2011-01-30 22:04
浏览 18
已采纳

PHP:如果具有不同的值,如何从子类访问受保护的变量?

How can a protected variable be accessed from a child Class if it has a different value?

Example to wrong access: parent::$_my gives error

class Father{
  protected $_my=array('a','b');
}

class Child{
   protected $_my=array('c','d');
  function __construct(){
   parent::__construct();
   $this->_my=array_merge(parent::$_my,$this->_my);
  }
}

Thanks, Yosef

  • 写回答

2条回答 默认 最新

  • duanhan9334 2011-01-30 22:07
    关注

    $this->_my will be inherited from the parent when you instantiate a subclass, so you simply need to use:

    $this->_my = array_merge($this->_my, array('c','d'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?