Use of class inheritance in PHP object-oriented

Source: Internet
Author: User
The inheritance of PHP classes is an important knowledge point in PHP language learning. So how can we correctly learn the inheritance of PHP classes? In PHP object-oriented programming, class inheritance is always the most critical. This is just like a human being giving birth to a child (why do we have to give birth to a child? Is it just to prevent the elderly! Don't know), you put some of your genes and You "> <LINKhref =" http://www.php100.c

The inheritance of PHP classes is an important knowledge point in PHP language learning. So how can we correctly learn the inheritance of PHP classes? In PHP object-oriented programming, class inheritance is always the most critical.

This is just like a human being giving birth to a child (why do we have to give birth to a child? Is it just to prevent the elderly! I don't know.) You take out some of your genes and your wife's genes and regenerate them into a new individual. This new personality will certainly contain the characteristics of both of you, this is a biological explanation of inheritance. In the world of programming, this inheritance is inheritance!

First of all, after learning some of the living principles of inheritance, I want to see if the inheritance of PHP classes is no longer so mysterious. Maybe it is not mysterious because we are too complicated. If there is inheritance, there must be a "root". You may imagine that you have a son or daughter in the future. They will get something (attributes and methods) from you) ", so your" Descendant "is holding all your (Root) features. The following describes how to express this object in PHP through syntax (it is impossible to be as direct as a human being. If you get married, your future generations will be generated after a while)

1. Generate a "root" Class (parent class or base class)

Syntax: class father {

}

1. Generate "Descendant" (subclass)

Syntax: class son extends father {

}

Note: the parent class is just a common class. If you want to have future generations, you only need to add an extends keyword after the normal class, in this way, your subclass only has all the attributes and methods of the parent class. It's actually that simple.

Let's do something practical. After all, it is necessary to define a parent class and a subclass in the inheritance of PHP classes to complete a task! In this case, the task is monotonous. For example, a person has a name (attribute), and a person wants to go to bed and eat (method ). Let's use this basic task to complete the knowledge in this section.
Class father {
Protected $ name;
Function _ construct ($ name ){
$ This-> name = $ name;
}
Function _ destruct (){
Echo"

{$ This-> name} is also dying.

";
}
// This is the so-called constructor used for initialization.
Function go_to_sleeping (){
Echo"

{$ This-> name} wants to go to bed.

";
}
Function eat (){
Echo"

{$ This-> name} wants to eat.

";
}
}
Class son extends father {
Function playing (){
// The child will be very naughty. Of course, he is also a creature to be sleeping.
Echo"

{$ This-> name} is playing tricks...

";
}
}
$ Your_father = new father ("dad ");
$ Your_father-> go_to_sleeping ();
$ Your_father-> eat ();
$ My_son = new son ('baby ');
$ My_son-> go_to_sleeping ();
$ My_son-> eat ();
$ My_son-> playing ();
?>
Class father {
Protected $ name;
Function _ construct ($ name ){
$ This-> name = $ name;
}
Function _ destruct (){
Echo"

{$ This-> name} is also dying.

";
}
// This is the so-called constructor used for initialization.
Function go_to_sleeping (){
Echo"

{$ This-> name} wants to go to bed.

";
}
Function eat (){
Echo"

{$ This-> name} wants to eat.

";
}
}
Class son extends father {
Function playing (){
// The child will be very naughty. Of course, he is also a creature to be sleeping.
Echo"

{$ This-> name} is playing tricks...

";
}
}
$ Your_father = new father ("dad ");
$ Your_father-> go_to_sleeping ();
$ Your_father-> eat ();
$ My_son = new son ('baby ');
$ My_son-> go_to_sleeping ();
$ My_son-> eat ();
$ My_son-> playing ();
?>
Resolution: In our first example of using inheritance, we used the constructor mentioned in the PHP constructor and the keyword in the PHP class encapsulation, if you don't understand anything, check it out! I don't want to talk about it anymore. I don't want to go to bed at noon. Let's talk about this mini program.

In the father class, we define general features, such as the name of a person, who wants to eat and sleep, and then in its subclass (descendant) we define a personalized method (playing). After all, there are differences between people. We use the constructor to initialize this person name. Of course, we also use the destructor to "destroy" the object, however, you may not find that there is no constructor or destructor in the subclass, so the subclass inherits all the methods of the parent, otherwise, how can you call $ my_son-> go_to_sleeping ()? This is the inheritance of the PHP class.

From: http://club.topsage.com/thread-501298-1-1.html

Class my_class {
Var $ username = "chick ";
Var $ num = 10;
Function yingxiaoji (){
// Use global variables in the class
$ Num = $ this-> num;
$ Username = $ this-> username;
Echo "I have". $ num. ". $ username;
}
Function jiegou (){
// When using functions in the class, use this to directly reference
$ This-> yingxiaoji ();
Echo "All sold ";
}
}
// The extension of the php class indicates inheritance, but php cannot inherit multiple base classes at the same time.
Class my_class_extend extends my_class {

Var $ color = "red ";
Function mycolor (){
$ Color = $ this-> color;
$ Username = $ this-> username;
Echo ", this is". $ username. "Yes". $ color;
}
}
$ Myname = new my_class (); // class instantiation
$ Myname-> yingxiaoji ();
Echo"
";

// Re-assign values to variables in the class
$ Myname-> username = "Duck ";
$ Myname-> num = "20 ";
$ Myname-> yingxiaoji ();
Echo"
";

$ Myname-> jiegou ();
Echo"
";

// Initialize the inheritance class of a class
$ Myname_extend = new my_class_extend ();
$ Myname_extend-> jiegou ();
$ Myname_extend-> mycolor ();

Echo"
";
// Because it is an inherited class, all the attributes of its base class are available, so you can directly pass a value to the base class variable.
$ Myname_extend-> username = "Duck ";
$ Myname_extend-& gt; num = 100;
$ Myname_extend-> color = "black ";
$ Myname_extend-> jiegou ();
$ Myname_extend-> mycolor ();
?>

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.