<?php class Par { private $data; public function setParentData() { $this->data = 'Parent'; } public function showParentData() { echo 'Par::data = ', $this->data, '<br>'; echo 'I know you have $', $this->money, '.<br>'; echo 'But I don\'t know your data.<br>'; } }
class Me extends Par { public $data; public $money; public function setData() { $this->data = 'Me'; $this->money = 63; } public function showData() { echo 'Me::data = ', $this->data, '<br>'; echo 'Me::money = ', $this->money, '<br>'; } }
$me = new Me(); $me->setData(); $me->setParentData(); $me->showParentData(); $me->showData();
输出: Par::data = Parent I know you have $63. But I don't know your data. Me::data = Me Me::money = 63
所以,当父类的data变量为private时,父类不能访问子类的同名变量data,子类也不能通过parent::$data访问父类的data变量。
|