Home  >  Article  >  Backend Development  >  PHP object-oriented guide (8) Overloading new methods_PHP tutorial

PHP object-oriented guide (8) Overloading new methods_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:58749browse

12. Overload new methods
When learning PHP, you will find that methods in PHP cannot be overloaded. The so-called method overloading means
defining the same method Name, through different "number of parameters" or different "type of parameters", to access different methods of our same method
name. However, 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, it can be called by passing different numbers of parameters.
Different methods with different method names are also not valid. So there is no method overloading in PHP. It cannot be overloaded, which means that methods with the same method name cannot be defined in
your project. In addition, because PHP does not have the concept of name subspace, methods with the same name cannot be defined on the same page
and included pages, nor can they be defined with the same name as the method provided by PHP, of course
Methods with the same name cannot be defined in the same class.
What do we mean by overloading new methods here? In fact, what we call overloading a new method is that the subclass
overrides the existing method of the parent class, so why do we do this? Can’t the methods of the parent class be inherited and used directly? But
there are some situations that we must cover. For example, in the example we mentioned earlier, the human being "Person"
has a "speak" method, and all those who inherit the "Person" class All subclasses can "speak". Our "Student"
class is a subclass of the "Person" class, so instances of "Student" can "speak", but humans "speak
words" The method states the attributes in the "Person" class, and the "Student" class extends the "Person" class and several new attributes. If you use the inherited "say ()" speaking method, you can only speak those attributes inherited from the
"Person" class, but the newly extended attributes cannot be spoken using the inherited "say()"
method. Yes, some people asked, if I define a new method in the "Student" subclass for
to speak, wouldn't it be enough to tell all the attributes in the subclass? Be sure not to do this. From an abstract point of view, a "student" cannot have two ways of "speaking". Even if you define two different speaking methods, you can achieve what you want
Function, the inherited "talk" method may not be used anymore, and you cannot delete it even if it is inherited.
At this time we will use overlay.
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
, so that the methods inherited from the parent class The method is overridden.
Code snippet



Copy code

The code is as follows: //Define a "person" Class as the parent class
class Person{
//The following are the member attributes of the person
var $name; //The person’s name
var $sex; //The person’s gender
var $age; //Age of a person
//Define a constructor parameter to assign values ​​to the attributes name $name, gender $sex and age $age
function __construct($name, $sex, $age){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//This person A way to speak, tell your own attributes
function say() {
echo "My name is: ".$this->name." Gender: ".$this->sex. " My age is: ".$this->age."
";
}
}
class Student extends Person
{
var $school; // Attributes of the school where the student is located
//How this student studies
function study() {
echo "My name is: ".$this->name." I am studying ".$this ->school."Learning
";
}
//This is a method that can talk about learning. It tells all its attributes and overrides the method of the same name in the parent class
function say( ) {
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age." I go to school at
".$this->school.".
";
}
}
?>


In the above example, we have overwritten the "say()" method inherited from the parent class in the "Student" subclass. By
overwriting, we have achieved the extension of the "method".
However, although doing this solves the problem we mentioned above, in actual development, a method cannot
consist of just one code or several codes, such as "say(" in the "Person" class )" method has 100 lines of code in it. For example
if we want to overwrite this method and retain the original functions plus a little bit more, we have to rewrite the original 100 lines of code
once and add The several lines of code extended above are pretty good, but 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, take the original functions of the overridden method and add your own
Function, you can use two methods to call 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 "parent::" method to call the overridden method in the parent class;
Code snippet
Copy code The code is as follows :

class Student extends Person{
var $school; //Attributes of the school where the student is located
//How this student learns
function study() {
echo "My name is: ".$this->name." I am studying at ".$this->school."
";
}
//This academic ability is OK The speaking method, tells all its attributes, overriding the parent class's method with the same name
function say() {
//Use the parent class's "class name::" to call the overridden method in the parent class ;
// Person::say();
//Or use "parent::" method to call the overridden method in the parent class;
parent::say();
//Add some of your own functions
echo "My age is: ".$this->age." I go to school in ".$this->school.".
";
}
}

Now you can access the overridden methods in the parent class in two ways. Which method is best for us? 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 in the extends declaration of the child
class. 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.
Similarly, if the constructor is not declared in the subclass, you can also use the constructor in the parent class. If a constructor is redefined in the subclass
, it will also override the constructor in the parent class. If you want to use the new constructor to assign values ​​to all
properties, you can use the same method.
Code snippet
Copy code The code is as follows:

class Student extends Person{
var $school; / /Attributes of the student’s school
function __construct($name, $sex, $age, $school){
//Use the method in the parent class to assign values ​​to the original attributes
parent::__construct( $name, $sex, $age);
$this->school=$school;
}
//How this student learns
function study() {
echo " My name is: ".$this->name." I am studying at ".$this->school."
";
}
//How this person can talk , say your own attributes
function say() {
parent::say();
//Add some of your own functions
echo "My age is: ".$this- >age."I go to school in ".$this->school.".
";

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320638.htmlTechArticle12. Overloading new methods When learning PHP, you will find that the methods in PHP cannot Overloading, the so-called method overloading is to define the same method name, through the "number of parameters"...
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