protected函数中含有private属性,此类被继承后,此属性是否有效

答案是肯定的,以下面代码为例:

  1. < ?php
  2.  class A
  3.  {
  4.      private $var;
  5.      protected function fun()
  6.      {
  7.          $this->var = ‘Hello var!’;
  8.          echo $this->var;
  9.      }
  10.  }
  11.  class B extends A
  12.  {
  13.      public function fun0()
  14.      {
  15.          $this->fun();
  16.      }
  17.  }
  18.  $b=new B();
  19.  $b->fun0();
  20. ?>

其中,var为class A中私有变量,被protected型fun函数调用。当class A被class B继承,class B并不能继承属性var,但是class B调用fun函数时,属性var仍然是有效的。 浏览器中显示结果如下:

[plain] view plaincopy

  1. Hello var!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注