What does php method rewriting mean?

藏色散人
Release: 2023-03-07 19:26:02
Original
2595 people have browsed it

php method rewriting refers to overriding the methods inherited from the parent class, that is, overriding the parent class method. The method is as follows: first define a constructor parameter; then define a subclass to use " extends" keyword to inherit; finally override the parent class method.

What does php method rewriting mean?

Recommended: "PHP Video Tutorial"

  • This method is suitable for any brand Computer

PHP object-oriented programming (OOP) method override operation example

This article describes PHP with examples Method override operation in object-oriented programming (OOP). Share it with everyone for your reference, the details are as follows:

Because PHP is a weakly typed language, it can receive different types of data in the parameters of the method itself, and because the PHP method can receive an indefinite number of Parameters, so it is not valid to call different methods with different method names by passing different numbers of parameters, so PHP cannot be overloaded.

Although methods with the same name cannot be defined in PHP, in the two classes with a parent-child relationship, we can define methods with the same name as the parent class in the subclass, thus inheriting the methods from the parent class. The method is overwritten (overriding the parent class method).

<?php
class Person
{
//下面是人的成员属性
var $name; //人的名子
var $sex; //人的性别
var $age; //人的年龄
//定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值
function __construct($name, $sex, $age) {
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
//这个人可以说话的方法, 说出自己的属性
function say() {
echo "my name is:" . $this->name . " sex:" . $this->sex . " my age is:" . $this->age;
}
}
//定义一个子类“学生类“使用”extends”关键字来继承”人”类:
class Student extends Person
{
var $school; //学生所在学校的属性
function __construct($name,$sex,$age,$school)
{
parent::__construct($name,$sex,$age);
$this->school = $school;
}
// 重写父类的say()方法
function say()
{
echo "my name is:" . $this->name ." my school is:" . $this->school;
}
//这个学生学习的方法
function study()
{
echo "my name is:" . $this->name . " my school is:" . $this->school;
}
}
?>
Copy after login

In addition, when a subclass overrides a method of a parent class, you should also pay attention to it. The access rights of the methods in the subclass must not be lower than the access rights of the overridden method of the parent class, that is, it must be higher than or equal to Access permissions for parent class methods.

For example, if the access permission of the parent class method is protected, then the permissions to be overridden in the subclass must be protected and public. If the parent class method is public, then the method to be overridden in the subclass can only be Public, in short, methods in subclasses always have higher access rights than or equal to the overridden methods of the parent class.

In the above example, we overwrote the "say()" method inherited from the parent class in the "Student" subclass. By overwriting, we achieved the extension of the "method". However, although doing this solves the problem we mentioned above, in actual development, a method cannot be just one code or several codes. For example, the "say()" method in the "Person" class has 100 lines of code. If we want to overwrite this method and retain the original functions plus a little bit more functionality, we have to rewrite the original 100 lines of code, plus a few expanded lines of code. This is pretty good. In some cases, the methods 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 to use the "parent::" method to call the overridden method in the parent class;

//定义一个子类“学生类“使用”extends”关键字来继承”人”类:
class Student extends Person
{
var $school; //学生所在学校的属性
function __construct($name,$sex,$age,$school)
{
parent::__construct($name,$sex,$age);
$this->school = $school;
}
// 重写父类的say()方法
function say()
{
//使用父类的"类名::"来调用父类中被覆盖的方法;
// Person::say();
//或者使用"parent::"的方试来调用父类中被覆盖的方法;
parent::say();
echo "my name is:" . $this->name ." my school is:" . $this->school;
}
//这个学生学习的方法
function study()
{
echo "my name is:" . $this->name . " my school is:" . $this->school;
}
}
Copy after login

Now you can access the overridden method in the parent class in two ways, which one we choose? What's the best way? Users may find that the code they write accesses the variables and functions of the parent class. This is especially true if the subclass is very refined or the parent class is very specialized. Do not use the literal name of the parent class in the code. Instead, use the special name parent, which refers to the name of the parent class pointed to by the subclass in the extends declaration. Doing this avoids using the parent class's name in multiple places. If the inheritance tree needs to be modified during implementation, simply modify the extends declaration in the class.

The above is the detailed content of What does php method rewriting mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template