


In PHP, you will encounter such a situation. The subclass inherits the parent class, but you need to make certain attributes and methods of the parent class. extension, then the subclass can rewrite the properties and methods, covering the properties and methods with the same name as the parent class. However, if the content of the parent class's method is relatively large, such as hundreds or thousands of lines of code, then only Use "parent class name::method" or "parent::method" to call the overridden method in the parent class , the only thing is to reload the parent class, and then add the statements that need to be expanded.
Overriding of methods
<?php class Person{ public $name; public function construct($name="" ){ $this->name=$name; } public function say(){ echo "我叫".$this->name ; } }?><?php class Student extends Person{ public $name; public function construct($name=""){ $this->name =$name; }//这里定义了一个和父类中同名的方法,将父类中的说话方法覆盖并重写 public function say(){ echo "我叫".$this->name .",今年25岁了" ; } }?>
Overriding methods and access rights
When a subclass overrides a method of the parent class, please note that the access rights of the method overridden in the child class must not be lower than the access rights of the overridden method of the parent class. For example, the access permission of a method in the parent class is protected, then the permission of the method overridden in the subclass must be protected or public. If the method of the parent class has public permission, the method to be overridden in the subclass can only be public. In short, when overriding a method of the parent class in a subclass, it must be higher than the permissions of the overridden method of the parent class.
Number of parameters when overriding
Subclasses can have a different number of parameters than the parent class, such as the following Constructor method In , an additional parameter $age is added.
<?phpclass Student extends Person{ public $name; public $age; public function construct($name="",$age=25){ $this->name =$name; $this->age =$age; } public function say(){ echo "我叫".$this->name .",今年".$this->age."岁了" ; } }?>
In the above example, we have achieved the extension of "method" by overriding it.
However, although doing this solves the problem we mentioned above, in actual development, a method cannot be just one code or several codes, such as the "say()" method in the "Person" class There are 100 lines of code in it. If we want to overwrite this method and retain the original functions and add a little more functionality, we have to rewrite the original 100 lines of code, plus a few expanded lines of code. This is still Okay, but in some cases, the method in the parent class cannot see the original code. How do you rewrite the original code at this time? We also have a solution, that is, in the subclass method, you can call the overridden method in the parent class. That is, you can take the original functions of the overridden method and add some of your own functions. There are two methods. Implement the method of calling the overridden method of the parent class in the method of the subclass:
One is to use the "class name::" of the parent class to call the overridden method of the parent class;
One is Use the "parent::" method to call the overridden method in the parent class;
Extension of the method
<?phpclass Student extends Person{ public $name; public $age; public function construct($name="",$age=25){ parent::construct($name,$age); $this->age =$age; } public function say(){ parent::say(); echo ",今年".$this->age."岁了" ; } }?>
What is described above is just reloading the attributes and methods of the parent class. It is not overloading in the true sense. It can only be said that the subclass is reloading the parent class. Expanded, in PHP, the word overloading also exists, but the meaning of overloading is different from that in general language-oriented languages.
The above is the detailed content of PHP object-oriented - sample code sharing of subclass extending parent class (subclass reloading parent class). For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools