Method Rewrite (override) Operation Example for PHP Object Oriented Programming (OOP)

  • 2021-11-13 00:56:01
  • OfStack

This paper describes the method rewrite (override) operation of PHP object-oriented programming (OOP). Share it for your reference, as follows:

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

Although we can't define a method with the same name in PHP, in the two classes with parent-child relationship, we can define a method with the same name as the parent class in the subclass, thus overwriting the inherited method in the parent class (overriding the parent class method).


<?php
class Person
{
// The following are the member attributes of people 
var $name; // The name of a man 
var $sex; // The sex of a person 
var $age; // The age of a person 
// Definition 1 Constructor parameters are property names $name , gender $sex And age $age Perform an assignment 
function __construct($name, $sex, $age) {
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
// The way this person can talk ,  Say your own attributes 
function say() {
echo "my name is:" . $this->name . " sex:" . $this->sex . " my age is:" . $this->age;
}
}
// Definition 1 Subclass "Student Class" uses " extends "Keyword to inherit the" person "class: 
class Student extends Person
{
var $school; // Attributes of the school where students are located 
function __construct($name,$sex,$age,$school)
{
parent::__construct($name,$sex,$age);
$this->school = $school;
}
//  Override the parent class's say() Method 
function say()
{
echo "my name is:" . $this->name ." my school is:" . $this->school;
}
// This student's method of learning 
function study()
{
echo "my name is:" . $this->name . " my school is:" . $this->school;
}
}
?>

In addition, when the subclass overwrites the method of the parent class, we should also pay attention to one point. The access permission 1 of the method in the subclass must not be lower than the access permission of the overwritten method of the parent class, that is, 1 must be higher than or equal to the access permission of the method of the parent class.

For example, if the access rights of the parent class method are protected The permissions to be overridden in the subclass are protected And public If the method of the parent class is public, the method to be overridden in the subclass can only be public In summary, the method in the subclass is always higher than or equal to the access rights of the overridden method of the parent class.

In the above example, we override the method of inheriting the method of "say ()" in the parent class in the subclass of "Student", and we extend the "method" by overriding. However, although doing so solves the problems we mentioned above, But in actual development, A method can't be just one code or several codes. For example, the "say ()" method in the "Person" class has 100 codes in it. If we want to overwrite this method and keep the original function and add a little function, we must rewrite the original 100 codes once, and add a few extended codes, which is still good. In some cases, the methods in the parent class can't see the original code. How can you rewrite the original code at this time? We also have a solution, that is, we can call the overridden method in the parent class in the subclass method, that is, we can take the original function of the overridden method and add our own 1-point function, and we can call the overridden method of the parent class in the subclass method through two methods:

1 is to use the parent class " 类名:: "To call the overridden method in the parent class; One is to use " parent:: "To call the overridden method in the parent class;

// Definition 1 Subclass "Student Class" uses " extends "Keyword to inherit the" person "class: 
class Student extends Person
{
var $school; // Attributes of the school where students are located 
function __construct($name,$sex,$age,$school)
{
parent::__construct($name,$sex,$age);
$this->school = $school;
}
//  Override the parent class's say() Method 
function say()
{
// Object of the parent class " Class name ::" To call the overridden method in the parent class; 
// Person::say();
// Or use "parent::" To call the overridden method in the parent class; 
parent::say();
echo "my name is:" . $this->name ." my school is:" . $this->school;
}
// This student's method of learning 
function study()
{
echo "my name is:" . $this->name . " my school is:" . $this->school;
}
}

Now overridden methods in the parent class can be accessed in both ways. Which is the best way for us to choose? 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 name on the parent class text in the code, but use a special name parent Which means that the subclass is in the extends The name of the parent class referred to in the declaration. This avoids using the name of the parent class in multiple places. If the inheritance tree is to be modified during implementation, simply modify the extends The part of the declaration.

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: