PHP Object-oriented full strategy (eight) Reload new method _php Tutorial

Source: Internet
Author: User
12. Overloading the New method
In the language of learning PHP you will find that the method in PHP is not overloaded, so-called method overloading is
Define the same method name, with different "number of parameters" or "type of parameter" to access our same method
Different methods of name. However, because PHP is a weakly typed language, it is possible to receive different classes in the parameters of the method itself.
Type of data, and because the PHP method can receive an indefinite number of parameters, so by passing different number of parameters called
Different methods of method names are also not valid. So there is no method overload in PHP. cannot be overloaded, that is,
You cannot define methods for the same method name in your project. Also, because PHP does not have the concept of name subspace, on the same page
The method of the same name cannot be defined in the polygon and the included page, and the method that PHP gives me
You cannot define a method of the same name in the same class.
What do we mean by the new method of overloading referred to here? In fact, the new way we call overloading is subclass
Overwrite the existing methods of the parent class, why do you do so? Can the parent class's methods not be inherited directly? But
There are some situations that we have to cover, such as the one we mentioned earlier, "person", the human being.
There is a "talking" method on the face, and all subclasses of the "person" class can "speak", we "Student"
Classes are subclasses of the "person" class, so instances of "Student" can "speak", but human beings "say
In the "Person" class, and the "Student" class expands the "person" class
New properties, if you use the inherited "Say ()" Method of speaking, you can only say from
Those attributes inherited by the "person" class, the newly extended attributes use the inherited "Say ()"
Method can not say, then some people asked, I in the "Student" this sub-class to define a new method for
To speak, to say all the attributes in the sub-class is all right? Do not do this, from an abstract point of view, a "learning
"You can't have two ways to speak", even if you define two different ways of speaking, you can achieve what you want.
function, inherited by the "talking" method may not have the opportunity to use, and is inherited from you also can not remove.
We're going to have to cover this time.
Although there is no way to define a method with the same name in PHP, in the two classes of a parent-child relationship, we can
Defines a method with the same name as the parent class, which overrides the method inherited from the parent class.
Code Snippets
Copy CodeThe code is as follows:
Define a "human" class as the parent class
Class person{
The following is the person's member property
var $name; The name of the man
var $sex; Man's Sex
var $age; The age of the person
Define a constructor method parameter to assign a property name $name, gender $sex, and age $age
function __construct ($name, $sex, $age) {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak in a way that speaks his own attributes
function say () {
echo "My name is called:". $this->name. "Gender:" $this->sex. "My Age is:". $this->age. "
";
}
}
Class Student extends Person
{
var $school; The properties of the school where students are located
The way the student learns
Function study () {
echo "My name is called:". $this->name. "I am". $this->school. " Learn
";
}
This learning can be a way to speak, to say all of their properties, covering the parent class with the same name method
function say () {
echo "My name is called:". $this->name. "Gender:" $this->sex. "My Age is:". $this->age. " I'm here
". $this->school." School.
";
}
}
?>

In the example above, we covered the "Say ()" method of inheriting the parent class in the "Student" subclass by
Overlay we have implemented the "method" extension.
But, like this solves the problem that we say above, but in the actual development, one method cannot be
A code or several code, such as the "person" class inside the "Say ()" Method has 100 code inside, such as
We would like to overwrite this method with preserving the original function, plus a little bit of functionality, it is necessary to rewrite the original 100 code
Once, plus a few extensions of the code, this is good, and in some cases, the method in the parent class is invisible to the original code
, how do you rewrite the original code at this time? We also have a solution, that is, in the subclass of this method can be
To invoke the overridden method in the parent class, that is, to bring the existing function of the overridden method to a point
function, you can implement methods that call the parent class in a method of a subclass in two ways:
One is to use the parent class's "Class Name::" To invoke the overridden method in the parent class;
One is to invoke the overridden method in the parent class using the "Parent::" Side test;
Code Snippets
Copy CodeThe code is as follows:
Class Student extends person{
var $school; The properties of the school where students are located
The way the student learns
Function study () {
echo "My name is called:". $this->name. "I am". $this->school. " Learn
";
}
This learning can be a way to speak, to say all of their properties, covering the parent class with the same name method
function say () {
Use the parent class's class name:: To invoke the overridden method in the parent class;
Person::say ();
Alternatively, use the "Parent::" Side test to invoke the overridden method in the parent class;
Parent::say ();
Add a bit of your own functionality
echo "My Age is:" $this->age. " I'm in ". $this->school." School.
";
}
}

Now you have access to the overridden methods in the parent class in both ways, which is the best way to choose? Users may
You will find yourself writing code that accesses the variables and functions of the parent class. If the subclass is very refined or the parent is very specialized
especially so. Do not use the name of the parent text in the code, should use the special name parent, it refers to the child
The name of the parent class that the class refers to in the extends declaration. This avoids using the name of the parent class in multiple places. If the following
The bearing tree is modified in the process of implementation, as long as the part of the extends declaration in the class is simply modified.
Similarly, a construction method can use a constructor in a parent class if it is not declared in a subclass, if the child class
Also overrides the constructor method in the parent class if you want to use the new constructor method for all
Property assignments can also be used in the same way.
Code Snippets
Copy CodeThe code is as follows:
Class Student extends person{
var $school; The properties of the school where students are located
function __construct ($name, $sex, $age, $school) {
Assign a value to an existing property by using a method in the parent class
Parent::__construct ($name, $sex, $age);
$this->school= $school;
}
The way the student learns
Function study () {
echo "My name is called:". $this->name. "I am". $this->school. " Learn
";
}
This person can speak in a way that speaks his own attributes
function say () {
Parent::say ();
Add a bit of your own functionality
echo "My Age is:" $this->age. " I'm in ". $this->school." School.
";

http://www.bkjia.com/PHPjc/320638.html www.bkjia.com true http://www.bkjia.com/PHPjc/320638.html techarticle 12. Overloaded new methods in the language of learning PHP you will find that PHP methods are not overloaded, so-called method overloading is to define the same method name, through the "number of parameters" ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.