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!

发表评论

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

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax