- <?php
- class A{
- public$a1='a1';
- protected$a2='a2';
- function test(){
- echo"hello!<hr/>";
- }
- }
- class B extends A{ //If class A and class B are not in the same file, please include and then operate
- public$a1='b1';
- function test2(){
- $this->test();
- parent::test(); //Subclass calls parent class method
- }
- function test()
- {
- echo$this->a1.',';
- echo$this->a2.',';
- echo"b2_test_hello<hr/>";
- }
- }
- $a = new B();
- $a->test();//b1,a2,b2_test_hello
- $a->test2();//b1,a2,b2_test_hello//hello!
- ?>
Method call: $this->method name(); If there is this method in the subclass, the method in the subclass is called, if not, the
parent:: in the parent class is called, and the method in the parent class is always called Methods.
Variable call: $this->variable name; if there is this variable in the subclass, it will be called in the subclass, if not, it will be called in the parent class