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

Mar 03, 2021 pm 05:20 PM
override php rewrite

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
?>
Copy after login

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>";
?>
Copy after login

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(){}				//错误:控制权比父类更严格
}
?>
Copy after login

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){}			//错误,与父类同名方法不一致
}
?>
Copy after login

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;;
    }
}
?>
Copy after login

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;;
    }
}?>
Copy after login

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 of this Website
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles