Php reloads new methods and class inheritance

Source: Internet
Author: User

Methods in PHP cannot be overloaded. The so-called method overload means defining the same method name. The number of parameters is different or the type of parameters is different, to access different methods with the same method name. inheritance is one of the important features of php5 surface image object programming. It refers to the creation of a new derived class, data and functions are inherited from one or more previously defined classes, and new data and functions can be redefined or added to create a class hierarchy or level.

Reload New Methods


When learning PHP, you will find that methods in PHP cannot be reloaded. The so-called method reload means defining the same method name, you can access different methods with the same method name by using "different number of parameters" or "different parameter types. However, because PHP is a weak language, different types of data can be received in method parameters, and PHP methods can receive parameters of an indefinite number, therefore, it is not true to call different methods with different method names by passing different numbers of parameters. So there is no method to overload in PHP. It cannot be overloaded, that is, you cannot define methods with the same method name in your project. In addition, because PHP does not have the concept of name sub-space, the same name method cannot be defined on the same page and the contained page, you cannot define a method with the same name as the method provided by PHP. Of course, you cannot define a method with the same name in the same class.

What do we refer to here for the new method of overloading? In fact, the new method of overloading is that the subclass overwrites the existing methods of the parent class. Why should we do this? Can the methods of the parent class be inherited and used directly? However, there are some situations that must be covered. For example, in the example we mentioned above, "Person" is a "speaking" method in humans, all subclasses that inherit the "Person" class can be "spoken". Our "Student" class is a subclass of the "Person" class, so the "Student" instance can "speak", but the "speak" method in humans says the attributes in the "Person" class, the "Student" class extends the "Person" class and extends several new attributes. If you use the inherited "say ()" speaking method, only the attributes inherited from the "Person" class can be said, so the newly extended attributes cannot use the inherited "say ()" method, some people have asked, I have defined a new method in the "Student" sub-class for speaking. Isn't it enough to say all the attributes in the sub-class? Do not do this. From an abstract point of view, a "student" cannot have two "speaking" methods. Even if you define two different speaking methods, you can implement the functions you want. The inherited "speaking" method may not be used, and you cannot delete the inherited method. In this case, we will use the overwrite method.

Although methods with the same name cannot be defined in PHP, in the parent-child relationship, we can define methods with the same name as the parent class in the subclass, in this way, the inherited methods in the parent class are overwritten.

The Code is as follows: Copy code

// Define a "person" class as the parent class
Class Person
{
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person

// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name, $ sex, $ age)
{
$ This-& gt; name = $ name;
$ This-& gt; sex = $ sex;
$ This-& gt; age = $ age;
}

// This person can talk about his/her attributes.
Function say ()
{
Echo "My name is :". $ this-& gt; name. "Gender :". $ this-& gt; sex. "My age is :". $ this-& gt; age. "& lt; br & gt ;";
}
}

Class Student extends Person
{
Var $ school; // attributes of the student's school

// How the student learns
Function study ()
{
Echo "My name is :". $ this-& gt; name. "I am ". $ this-& gt; school. "Learn & lt; br & gt ;";
}

// This learning method that can be used to speak out all its attributes, covering the same name method of the parent class
Function say ()
{
Echo "My name is :". $ this-& gt; name. "Gender :". $ this-& gt; sex. "My age is :". $ this-& gt; age. "I am ". $ this-& gt; school. "Go to school. & lt; br & gt ;";
}
}

In the above example, we overwrite the "say ()" method inherited from the parent class in the "Student" subclass, and implement the "method" extension by overwriting.

However, although this solution solves the problems we mentioned above, in actual development, it is impossible for a method to have one or several pieces of code, for example, there are 100 pieces of code in the "say ()" method in the "Person" class. If we want to overwrite this method to retain the original functions and add a little bit of functionality, it is better to rewrite the original 100 pieces of code and add several extensions. 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 can also solve this problem by calling the override method in the parent class in the subclass method, that is to say, you can add your own functions to the original functions of the method to be overwritten by the parent class in the subclass method in two ways:

One is to use the "Class Name:" of the parent class to call the override method in the parent class;
One is to use the "parent:" method to call the method that is overwritten in the parent class;

The Code is as follows: Copy code

Class Student extends Person
{
Var $ school; // attributes of the student's school

// How the student learns
Function study ()
{
Echo "My name is :". $ this-& gt; name. "I am ". $ this-& gt; school. "Learn & lt; br & gt ;";
}

// This learning method that can be used to speak out all its attributes, covering the same name method of the parent class
Function say ()
{
// Use the "Class Name:" of the parent class to call the override method in the parent class;
// Person: say ();

// Or use "parent:" to call the method that is overwritten in the parent class;
Parent: say ();

// Add your own functions
Echo "My age is :". $ this-& gt; age. "I am ". $ this-& gt; school. "Go to school. & lt; br & gt ;";
}
}

Now we can access the method that is overwritten in the parent class in both ways. Which of the following methods is the best? You may find that the code you 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 of the parent class in the code. You should use the special name parent, which refers to the name of the parent class that the subclass refers to in the extends declaration. This avoids the use of the parent class name in multiple places. If the inheritance tree needs to be modified during implementation, simply modify the part declared by extends 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 new constructor is defined in the subclass, it will overwrite the constructor in the parent class, you can assign values to all attributes using the new constructor in the same way.

The Code is as follows: Copy code

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 attribute
Parent: :__ construct ($ name, $ sex, $ age );
$ This-& gt; school = $ school;
}

// How the student learns
Function study ()
{
Echo "My name is :". $ this-& gt; name. "I am ". $ this-& gt; school. "Learn & lt; br & gt ;";
}

// This person can talk about his/her attributes.
Function say ()
{
Parent: say ();
// Add your own functions
Echo "My age is :". $ this-& gt; age. "I am ". $ this-& gt; school. "Go to school. & lt; br & gt ;";
}
}

Class inheritance


Inheritance, as one of the three important features of object-oriented, plays an important role in the Object-Oriented field. It seems that we have not heard of any object-oriented language that does not support inheritance. Inheritance is one of the important features of php5 object program design. It refers to the creation of a new derived class that inherits data and functions from one or more previously defined classes, in addition, you can redefine or add new data and functions to establish a class hierarchy or level. Simply put, inheritance is the mechanism by which child classes automatically share the data structure and method of the parent class. This is a relationship between classes. When defining and implementing a class, you can base on an existing class and take the content defined by this existing class as your own content, and add some new content. For example, you already have a "person" class, which contains two member attributes: "name and age" and two member Methods: "speaking and walking". if the current program requires a student class, because the student is also a person, the student also has the member attribute "name and age" and the member method "speaking method and walking method", at this time, you can let the student class inherit this class. After inheritance, the student class will inherit all the attributes in humans, you don't need to re-declare the attributes and methods of these Members, because there are also attributes and learning methods of the school in the student class, so in your student class, there are attributes and methods inherited from the human class, in addition to the student's unique "school attributes" and "learning methods", the declaration of such a student class is complete, and we can also call the subsequent letter "extension". From the above we can see that the student class has extended the human class, add an attribute and a method on the basis of two original attributes and two methods in humans to expand a new student class.

The Inheritance Mechanism allows you to use existing data types to define new data types. The defined new data type not only has the newly defined members, but also has the old members. We call the existing class used to derive the new class as the base class, also known as the parent class and super class. A new class derived from an existing class is called a derived class, or a subclass.

In software development, the inheritance of classes makes the software open and scalable. This is an effective method for information organization and classification. It simplifies the workload of object and class creation, added code reusability. Inheritance is used to provide a standard class level structure. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.

In C ++, a derived class can be derived from a base class or multiple base classes. Inheritance derived from a base class is called single inheritance; inheritance derived from multiple base classes is called multi-inheritance.

 

But in PHP and Java, there is not much inheritance, but only single inheritance. That is to say, a class can only inherit data from one class directly. This is what we call single inheritance.

 

For example, the following is an abstraction of the "person" class.

The Code is as follows: Copy code

// Define a "person" class as the parent class
Class Person
{
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person

// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name, $ sex, $ age)
{
$ This-& gt; name = $ name;
$ This-& gt; sex = $ sex;
$ This-& gt; age = $ age;
}

// This person can talk about his/her attributes.
Function say ()
{
Echo "My name is :". $ this-& gt; name. "Gender :". $ this-& gt; sex. "My age is :". $ this-& gt; age. "& lt; br & gt ;";
}
}

Next we will create a "Student Class". If it is not inherited as follows:

The Code is as follows: Copy code

// Define a "Student Class" as the parent class
Class Student
{
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
Var $ school; // attributes of the student's school

// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = "", $ sex = "", $ age = "", $ school = "")
{
$ This-& gt; name = $ name;
$ This-& gt; sex = $ sex;
$ This-& gt; age = $ age;
$ This-& gt; school = $ school;
}

// This person can talk about his/her attributes.
Function say ()
{
Echo "My name is :". $ this-& gt; name. "Gender :". $ this-& gt; sex. "My age is :". $ this-& gt; age. "& lt; br & gt ;";
}

// How the student learns
Function study ()
{
Echo "My name is :". $ this-& gt; name. "I am ". $ this-& gt; school. "Learning & lt; br & gt ;";
}
}


// Define a subclass "Student Class" using the "extends" keyword to inherit "person" Class
Class Student extends Person
{
// Attributes of the student's school
Var $ school;

// How the student learns
Function study ()
{
Echo "My name is:". $ this-> name. "I am". $ this-> school. "Learn <br> ";
}
}

Through the definition of the "Student" class above, the Student class inherits all the member attributes and member methods in the Person class by using the "extends" keyword, and expanded the attributes of a school member "school" and a learning method "study ()". the following attributes and methods are available for the objects in the subclass "Student" and the objects that use this class instance:

The member attributes in Student include:
Name: name;
Age: age;
Gender: sex;
School: school;
Student includes the following member methods:
Method of speaking: say ();
Learning Method: study ();

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.