search
HomeBackend DevelopmentPHP7If you have another chance, rewriting override is your way out

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use