Home >Backend Development >PHP7 >If you have another chance, rewriting override is your way out

If you have another chance, rewriting override is your way out

autoload
autoloadOriginal
2021-03-03 17:20:211922browse

Definition : Override, that is, a member with the same name as the parent class is defined in the subclass. The subclass can override any class member of the parent class. Override is usually used to override Methods of the parent class are used to extend or change some business logic.

1. Whether it is a public attribute or a protected attribute, once overrides, the parent class attribute will no longer be exists, and private properties will not be lost due to overwriting.

<?php
    class A{
      
        public $name=&#39;张三&#39;;
        protected $sex=&#39;man&#39;;
        private  $age=&#39;25&#39;;

        public function getName(){
           echo __CLASS__,&#39;<br/>&#39;;
           echo $this->name."<br>";
        }
        protected function getSex(){
            echo __CLASS__,&#39;<br/>&#39;;
            echo $this->sex."<br>";
        }
        private function getAge(){
            echo __CLASS__,&#39;<br/>&#39;;
            echo $this->age."<br>";
        }

    }
    class B extends A{
        
        public $name=&#39;王五&#39;;
        protected $sex=&#39;woman&#39;;
        private  $age=&#39;26&#39;;
    
        
        public function getAll(){
            echo $this->name."<br>";
            echo $this->sex."<br>";
            echo $this->age."<br>";
        }
    }

    $a=new B();
    var_dump($a);
    
  /* object(B)#1 (4) 
   { ["name"]=> string(6) "王五" 
     ["sex":protected]=> string(5) "woman" 
     ["age":"B":private]=> string(2) "26" 
     ["age":"A":private]=> string(2) "25"
   }*/
    
    echo "<br>";
    $a->getAll();//王五 woman 26
?>

It can be found that both public attributes and protected attributes are overwritten by , while private attributesBecause it is not inherited by , it is not affected.

<?php
    class A{
      
        public $name=&#39;张三&#39;;
        protected $sex=&#39;man&#39;;
        private  $age=&#39;25&#39;;

        public function getName(){
           
           echo $this->name."我是父类的getName"."<br>";
        }
        protected function getSex(){
            
            echo $this->sex."我是父类的getSex"."<br>";
        }
        private function getAge(){
            
            echo $this->age."我是父类的getAge"."<br>";
        }

    }
    class B extends A{
        
        public $name=&#39;王五&#39;;
        protected $sex=&#39;woman&#39;;
        private  $age=&#39;26&#39;;

        public function getName(){
           
           echo $this->name."我是子类的getName"."<br>";
        }
        protected function getSex(){
          
            echo $this->sex."我是子类的getSex"."<br>";
        }
        private function getAge(){
          
            echo $this->age."我是子类的getAge"."<br>";
        }
        public function getAll(){
            $this->getName();
            $this->getSex();
            $this->getAge();
        }
    }

    $a=new B();
    $a->getAll();//王五我是子类的getName woman我是子类的getSex 26我是子类的getAge
    echo "<br>";
?>

Summary: Overriding public and protected properties directly overrides parent class members, and private properties will not be overwritten; public and protected methods will be rewritten, but private methods will not Overridden (the nature of private methods is not inherited).

2. Subclasses are required to override parent class methods.

a. When a subclass overrides the method of the parent class, the control rights cannot be higher than that of the parent class, that is, the subclass can be more open than the parent class.

<?php
class Fu{
    protected function show(){
        echo __CLASS__,&#39;<br/>&#39;;
    }
}
class Zi extends Fu{
    protected function show(){}				//正确
    public function show(){}				//允许
    private function show(){}				//错误:控制权比父类更严格
}
?>

b. Rewriting in PHP requires that when a subclass rewrites a parent class method, it must ensure that the parameters of the method with the same name of the parent class are consistent.

<?php
class Fu{
    protected function show(){
        echo __CLASS__,&#39;<br/>&#39;;
    }
}
class Zi extends Fu{
    public function show(){}
    public function show($a){}			//错误,与父类同名方法不一致
}
?>

c. Rewriting is for inherited members. The private methods of the parent class will not be inherited, so they are not subject to requirement b.

<?php
class Fu{
    private function show(){
        echo __CLASS__,&#39;<br/>&#39;;
    }
}
class Zi extends Fu{
    private function show($name){		//不会报错,因为本质不存在重写(父类Fu::show没有被继承)
        echo $name,&#39;<br/>&#39;;
    }
}
?>

d. Rewriting refers to the special situation of the subclass. Generally, it needs to be extended on the basis of the parent class. At this time, if you want to continue to ensure that the parent class is rewritten The method continues to execute (by default, only new methods overridden by subclasses are always accessed). You need to use the parent keyword when rewriting methods in subclasses.

<?php
class Fu{
    protected function show(){
        echo __CLASS__,&#39;<br/>&#39;;
    }
}
class Zi extends Fu{
    public function show(){
        parent::show();
        
        //扩展业务逻辑
        echo __CLASS__,&#39;<br/>&#39;;
    }
}?>

Summary: parent cannot access the properties of the parent class, but can access static properties, static methods,Class constants and Common methods.

Recommended: php tutorial,php video tutorial

The above is the detailed content of If you have another chance, rewriting override is your way out. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more