PHP overloads new methods and classes of inheritance _php tutorial

Source: Internet
Author: User
PHP methods are not overloaded, so-called method overloading is to define the same method name, through the "number of parameters" different or "parameter type", to access our different methods of the same method name, inheritance is an important feature of PHP5 object programming, it refers to the establishment 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 hierarchies or levels of classes.

New methods for overloading


In the language of learning PHP you will find that the method in PHP can not be overloaded, so-called method overloading is to define the same method name, through the "number of parameters" different or "parameter type" different, to access our same method name different methods. However, because PHP is a weak type of language, so in the parameters of the method itself can receive different types of data, and because the PHP method can receive an indefinite number of parameters, so by passing different number of parameters to call the different method name is not true. So there is no method overload in PHP. You cannot overload a method that does not define the same method name in your project. Also, because PHP does not have the concept of a namespace, the same name cannot be defined in the same page and contained pages, nor does it define the same name as the method that PHP gives me, and of course the same names cannot be defined in the same class.

What do we mean by the new method of overloading referred to here? In fact, the new method of overloading is that the subclass overrides the existing methods of the parent class, so why do we do this? Can the parent class's methods not be inherited directly? But there are situations that we have to cover, like the example we mentioned earlier, where "person" has a "talking" method, and all subclasses of the "person" class can "speak", and our "Student" class is the subclass of the "person" class. So the example of "Student" can "speak", but the human inside the "talking" method inside the "person" class is the attribute, while the "Student" class on the "person" class expanded, but also extended several new properties, if the use of inherited "Say ( "To speak, you can only say those attributes inherited from the" person "class, then the newly extended attributes use this inherited" Say () "method can not be said, then some people asked, I in the" Student "this sub-class to define a new method for speaking, Is it OK to say all the attributes in the subclass? Must not do so, from an abstract point of view, a "student" can not have two ways to "talk", even if you define two different ways to speak, you can achieve the function you want, the inheritance of the "talking" method may not have the opportunity to use, and is inherited from you also deleted. 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 define a method in the subclass with the same name as the parent class, overwriting the inherited method in the parent class.

The code is as follows Copy Code
< p>//defines a "person" class as the parent class
class person
{
///The following is a member of the Human property
var $name;//person's name child
Var $sex;//Gender
Var $age;//person's age

//Define a constructor method parameter to assign values for 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 properties
function say ()
{
echo "My name is called". $this->name. "Gender:". $this->sex. " My age is: ". $this->age." <br> ";
}
}

Class Student extends person
{
var $school;//Student's School's properties

//The way this student learns
Function study ()
{
echo my name is called: $this->name. "I am". $this->school. "Learning <br>";
}

//This learning can be a way to speak, say all of their properties, overriding the parent class of the same name method
function say ()
{
echo "My name is called:". $this->name. "Gender:". $this->sex . "My age is:" $this->age. " I'm in ". $this->school." School .<br> ";
}
}

In the example above, we covered the "Say ()" method of inheriting the parent class in the "Student" subclass and implemented the "method" extension by overwriting us.

But, like this solves the problem that we said above, but in the actual development, a method can not be a code or a few code, such as "person" class inside the "Say ()" Method has 100 code inside, If we want to cover this method to preserve the original function plus a little bit of functionality, it is necessary to rewrite the original 100 code once, plus the extension of a few code, this is good, and in some cases, the method in the parent class is not see the original code, this time how do you rewrite the original code? We also have a solution, that is, in the subclass of this method can be called to the parent class is overridden by the method, that is, the overridden method of the original function to take over and add their own point of function, you can use two methods to implement the method in the subclass to call the parent class is overridden:

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;

copy code

Class Student extends person
{
var $ School Properties of the student's school

//The way this student learns
Function study ()
{
echo "My name is called:". $this->name. "I am". $this->school. " Learning <br> ";
}

//This is a way to speak, say all of your properties, override the parent class's method
function say ()
{
//Use the parent class's class name:: To invoke the overridden method in the parent class;
//person :: Say ();

//or use the "Parent::" Trial 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 .<br> ";
}
}

Now you have access to the overridden methods in the parent class in both ways, which is the best way to choose? Users may find themselves writing code that accesses the variables and functions of the parent class. This is especially true if subclasses are very refined or if the parent class is very specialized. Do not use the name on the parent literal in the code, and you should use the special name parent, which refers to the name of the child class in the extends 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 part of the extends declaration in the class.

Similarly, a construction method in a subclass can also use a constructor in a parent class if it is not declared, and if a constructor is redefined in a subclass, it overrides the constructor in the parent class, and you can use the new constructor method to assign values to all properties in the same way.

copy code

Class Student extends person
{
var $ School Student's School property

Function __construct ($name, $sex, $age, $school)
{
//Use a method in the parent class to assign a value to an existing property
Parent::__ Construct ($name, $sex, $age);
$this->school= $school;
}

//The way this student learns
Function study ()
{
echo "My name is called". $this->name. "I am". $this->school. "Learning < Br> ";
}

//This person can speak in a way that speaks his own properties
function say ()
{
Parent::say ();
//Add a bit of your own functionality
echo "My Age is:". $this- >age. " I'm in ". $this->school." School .<br> ";
}
}

Inheritance of Classes


Inheritance, an aspect of three important features of object-oriented, plays an important role in the field of object-oriented, and seems to have not heard of any object-oriented language that does not support inheritance. Inheritance is one of the important features of PHP5 object programming, which refers to the creation of a new derived class that inherits data and functions from one or more previously defined classes, and can redefine or add new data and functions, thus establishing the hierarchy or rank of the class. The simple point is that inheritance is the mechanism by which subclasses automatically share data structures and methods of the parent class, which is a relationship between classes. When defining and implementing a class, it can be done on top of an already existing class, taking the content defined by the existing class as its own content, and adding a number of new content. For example, you now have a "people" class, which has two member attributes "name and age" and there are two member methods "talking Method and Walking method", if the program needs a student's class now, because the student is also a person, so the student also has the member attribute "name and age" and the member method " The way of speaking and the way to walk ", this time you can let the student class to heir this class, after inheriting, the student class will inherit all the attributes of human beings, you do not have to re-declare these member properties and methods, because the student class also has the property of the school and learning methods, So in the student class you do have inherited from the human inside of the properties and methods in addition to the student's unique "School properties" and "learning method", such a student class declaration completed, the following letter we can also be called "extension", from the above we can see that the student class on human expansion, Based on the original two attributes and two methods in humans, add a property and a method to extend out a new class of students.

The inheritance mechanism allows you to define new data types by using existing data types. The new data type that you define has not only the newly defined members, but also the old members. We call the existing classes used to derive new classes as base classes, also known as parent classes, and superclass classes. A new class derived from an existing class is called a derived class, also known as a subclass.

In software development, the inheritance of the class makes the software open and extensible, which is an effective method of information organization and classification, which simplifies the creation of objects and classes, and increases the weight of the code. With inheritance, the hierarchical structure of the class's specification is provided. Through the inheritance of the class, the common features can be shared, and the reusability of the software is improved.

In the C + + language, a derived class can derive from a base class or from more than one base class. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.

But in PHP and the Java language there is no multiple inheritance, only a single inheritance, that is, a class can only directly inherit data from a class, this is what we call the single inheritance.

For example: The following is an abstraction of the "human" class

The code is as follows Copy Code

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. " <br> ";
}
}

Below we do a "student class", if not with inheritance as follows:

The code is as follows Copy Code

Define a "Student class" class as the parent class
Class Student
{
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
var $school; The properties of the school where students are located

Define a constructor method parameter to assign a property name $name, gender $sex, and age $age
function __construct ($name = "", $sex = "", $age = "", $school = "")
{
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
$this->school= $school;
}

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. " <br> ";
}

The way the student learns
Function study ()
{
echo "My name is called:". $this->name. "I am". $this->school. "Learning <br>";
}
}


Define a subclass "Student class" use the "extends" keyword to inherit the "People" class
Class Student extends Person
{
The properties of the school where students are located
var $school;

The way the student learns
Function study ()
{
echo "My name is called:" $this->name. "I am". $this->school. "Learn
";
}
}

Through the definition of the "Student" class above, the Student class inherits all the member properties and the member methods in the person class by using the keyword "extends", and expands a school member property "school", and a Learning Method "study () ". Now the subclass" Student "and objects that use this class instance have the following properties and methods:

The member attributes of the student class "Student" are:
Name: name;
Age: Ages;
Gender: Sex;
School: School;
The member methods of the student class "Student" are:
Method of speech: Say ();
Learning Method: study ();

http://www.bkjia.com/PHPjc/629200.html www.bkjia.com true http://www.bkjia.com/PHPjc/629200.html techarticle PHP methods are not overloaded, so-called method overloading is to define the same method name, through the "number of parameters" different or "parameter type" different, to access our same method ...

  • 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.