php私有成员private的程序题目

class base
{
  private $member;

  function __construct()
  {
      echo __METHOD__ . "(begin)\n";
    $this->member = ‘base::member‘;
    $this->test();
      echo __METHOD__ . "(end)\n";
  }

  function test()
  {
      echo __METHOD__ . "\n";
    print_r($this);
  }
}

class derived extends base
{
  public $member = ‘derived::member (default)‘;

  function __construct()
  {
      echo __METHOD__ . "(begin)\n";
      parent::__construct();
      parent::test();
    
      $this->test();
    $this->member = ‘derived::member‘;
      echo __METHOD__ . "(end)\n";
  }

  function test()
  {
      parent::test();
      echo __METHOD__ . "\n";
    print_r($this);
  }
}

$t = new derived();
$t->test();
unset($t);

打印结果为:

 1 derived::__construct(begin)
 2 base::__construct(begin)
 3 base::test
 4 derived Object
 5 (
 6     [member] => derived::member (default)
 7     [member:base:private] => base::member
 8 )
 9 derived::test
10 derived Object
11 (
12     [member] => derived::member (default)
13     [member:base:private] => base::member
14 )
15 base::__construct(end)
16 base::test
17 derived Object
18 (
19     [member] => derived::member (default)
20     [member:base:private] => base::member
21 )
22 base::test
23 derived Object
24 (
25     [member] => derived::member (default)
26     [member:base:private] => base::member
27 )
28 derived::test
29 derived Object
30 (
31     [member] => derived::member (default)
32     [member:base:private] => base::member
33 )
34 derived::__construct(end)
35 base::test
36 derived Object
37 (
38     [member] => derived::member
39     [member:base:private] => base::member
40 )
41 derived::test
42 derived Object
43 (
44     [member] => derived::member
45     [member:base:private] => base::member
46 )
47 Done

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。