First, we will post a visual, intuitive, and estimated crowd chart about public, protect, and private.
Scope |
Current class |
Same package |
Child class |
Other packages |
Public |
T |
T |
T |
T |
Protect |
T |
T |
T |
F |
Private |
T |
F |
F |
F |
T: True F: false
Now I will test and verify it. Others can be launched based on the above table.
Among the three, I think private is relatively complicated, so select private!
1. The subclass cannot inherit or directly access the private attributes and methods of the parent class,
A. if you attempt to modify the private attribute of the parent class in the subclass $ this-> variable = val;
1 <?php 2 class test { 3 private $variable = 1; 4 public function setVal($param) { 5 $this->variable = $param; 6 } 7 public function getVal() { 8 return $this->variable; 9 }10 private function output() {11 echo 1;12 }13 }14 class test2 extends test {15 public function __construct(){16 $this->variable =2;17 }18 }19 $obj = new test2();20 print_r($obj);21 echo ‘<br />‘;22 echo $obj->variable;23 //$obj->output();24 echo ‘<br />‘;25 echo $obj->getVal();26 echo ‘<br />‘;27 $obj->setVal(3);28 echo $obj->getVal();29 echo ‘<br />‘;30 print_r($obj);31 }32 ?>
Output:
Test2 object ([variable: Test: Private] => 1 [variable] => 2)
2
1
3
Test2 object ([variable: Test: Private] => 3 [variable] => 2)
We can see that private attributes cannot be directly modified or overwritten. If this write method only defines an attribute for the subclass, the program will not report an error, only the interface methods provided by the parent class are used to set the private attributes of the parent class. B, or try to overwrite the private attribute private variable = Val of the parent class; the program will report an error, for example:
1 <?php 2 class test { 3 private $variable = 1; 4 public function setVal($param) { 5 $this->variable = $param; 6 } 7 public function getVal() { 8 return $this->variable; 9 }10 private function output() {11 echo 1;12 }13 }14 class test2 extends test {15 public function __construct(){16 //$this->variable =2;17 private $variable = 2;18 }19 }20 $obj = new test2();21 print_r($obj);22 echo ‘<br />‘;23 echo $obj->variable;24 //$obj->output();25 echo ‘<br />‘;26 echo $obj->getVal();27 echo ‘<br />‘;28 $obj->setVal(3);29 echo $obj->getVal();30 echo ‘<br />‘;31 print_r($obj);32 ?>
Error:
Parse error: Syntax error, unexpected t_private in
D: \ www \ smarty_3 \ index. phpOn Line
19
2. If the subclass needs to modify the private attributes of the parent class, it must provide the modified interface in the parent class, that is, modifying the familiar public methods.
<?php class test { private $variable = 1; public function setVal($param) { $this->variable = $param; } public function getVal() { return $this->variable; } private function output() { echo 1; }}class test2 extends test { public function __construct(){ $this->variable =2; }}$obj = new test2();print_r($obj);$obj->setVal(3);echo $obj->getVal();echo ‘<br />‘;print_r($obj);} ?>
Zero o'clock AM at work, and the result was also seen by the leaders. I am so embarrassed that the format is not too careless ,,,,
Public, protect, and private are used in the parent class subclass.