Methods in PHP cannot be overloaded. The so-called method overload means defining the same method name. The number of quot; parameters is quot; different or quot; parameter types are quot; different, to access different methods with the same method name. The inheritance is php... methods in PHP cannot be overloaded. The so-called method overload means defining the same method name. it is different through "number of parameters" or "parameter type, 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 "number of parameters" or "type of parameters. 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 way to overload in PHP. it cannot be reloaded, 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". Student is a subclass of the "Person" class, so the "Student" instance can "speak", but the "speak" method in the human body 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, then some people asked, I defined a new method in the "Student" sub-class to talk about it. 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 it. 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 instance code is as follows:
Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // The way this person can speak, say your own property 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 Student's school // function study () {echo" My name is: ". $ this-> name. "I am ". $ this-> school. "Learning
";}// This learning method that can be used to speak out all its attributes, covering the function say () {echo" My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "I am ". $ this-> school. "go to school.
";}}?>
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 function 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 instance code is as follows:
Name. "I'm learning". $ this-> school ."
";}// 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 to be overwritten in the parent class; parent: say (); // add your own function echo" My age is :". $ this-> age. "I am ". $ this-> school. "go to school.
";}}?>
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, if you want to assign values to all attributes using the new constructor, you can use the same method.
The instance code is as follows:
School = $ school;} // function study () {echo "My name is :". $ this-> name. "I am ". $ this-> school. "Learning
";}// This person can speak in a way that says his own attribute function say () {parent: say (); // add your own function echo "My age is :". $ this-> age. "I am ". $ this-> school. "go to school.
";}}?>
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 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, therefore, in your student category, 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 based on 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. the existing classes used to derive new classes are called base classes, also known as parent classes and superclasses. 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 instance code is as follows:
Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // The way this person can speak, say your own property function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}}
Next we will create a "student class". if it is not inherited as follows:
The instance code is as follows:
Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age; $ this-> school = $ school ;} // This person can talk about the function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";}// The student's learning method function study () {echo" My name is :". $ this-> name. "I am ". $ this-> school. "Learning
";}}// Define a subclass" Student class "use the" extends "keyword to inherit from the" Person "class Student extends Person {// attributes of the Student's school var $ school; // function study () {echo "My name is :". $ this-> name. "I am ". $ this-> school. "Learning
";}}?>
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 extended a school member attribute "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 "Student" contains the member methods: say (); learning method: study ();
Tutorial URL:
You are welcome to add your _ favorites to the Favorites folder, but please keep the link for this article.