This is like a human having children (why must we have children, is it to prevent the old!) You have some of your genes and your wife's genes, and you have to regenerate them into a new individual, and this new personality will certainly contain the characteristics of the two of you, which is a biological interpretation of heredity (inheritance). In the world of programming is this heredity is inheritance!
First of all, after understanding some of the living principles of inheritance, I would like to look at the inheritance of the PHP class is not so mysterious. Perhaps it is not mysterious, because we are too complex. To inherit, you have to have a "root cause", the "root" you may imagine you have a son or daughter, they will get some "things (properties and methods)" from you, so that your "offspring" is holding all of your (root) characteristics. Here's a syntax to describe how this is expressed in PHP object-oriented (not as straightforward as human beings, married, and over time your offspring are produced)
1. Generate a "root" class (parent or base class)
Syntax: Class father{
}
1. Produce "Descendants" (subclass)
Syntax: class son extends father{
}
Description: The parent class is just a normal class, if you want to have descendants you just need to add a extends keyword after the normal class, so that your child class has only the parent class all the properties and methods. It's really that simple.
Let's do some practical things now, after all, the inheritance of the PHP class defines a parent class and subclass to complete a task! Here this task is more monotonous, on the person, people have a name (attribute), people have to sleep and eat (method). We will use this basic task to complete the knowledge of this section.
- < ? PHP
- Class father{
- protected $name;
- function __construct ($name) {
- $this- > name = $name;
- }
- function __destruct () {
- echo " < p>{$this->name} is also going to die < br/>< / p>";
- }
- This is called a constructor, which is used to initialize
- function go_to_sleeping () {
- echo " < p>{$this->name} want to sleep. < / P > ";
- }
- Function Eat () {
- echo " < p>{$this->name} want to eat. < / P > ";
- }
- }
- Class Son extends father{
- function playing () {
- The child will be very naughty, of course, he also wants to eat to sleep the creature
- echo " < p>{$this->name} is making Mischief ... < / P > ";
- }
- }
- $ 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 ();
- ?>
- < ? PHP
- Class father{
- protected $name;
- function __construct ($name) {
- $this- > name = $name;
- }
- function __destruct () {
- echo " < p>{$this->name} is also going to die < BR/>< / p>";
- }
- This is called a constructor, which is used to initialize
- function go_to_sleeping () {
- echo " < p>{$this->name} want to sleep. < / P > ";
- }
- Function Eat () {
- echo " < P > {$this->name} want to eat. p>";
- }
- }
- Class Son extends father{
- function playing () {
- The child will be very naughty, of course, he also wants to eat to sleep the creature
- echo " < p>{$this->name} is making Mischief ... < / P > ";
- }
- }
- $ 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 ();
- ?>
Parsing: In our first example of using inheritance, we used the constructors mentioned in PHP's constructor and the keywords in the package of PHP classes, and if there's anything you don't understand, go and see! I do not want to say more, noon did not want to sleep ah. Tell me about this little program.
In the father of the class, we define the general characteristics, such as the name of the person, the person to eat and sleep, and then in its subclasses (descendants) we define a personalized method (playing), after all, there is a different place between people. We use constructors to initialize this name, and of course use destructors to "destroy" objects, but you may not find that there are no constructors or destructors within subclasses, so subclasses are all ways to inherit from the parent, or else how can you $my_son->go_to_ Sleeping (); so called, this is the inheritance of the PHP class.
http://www.bkjia.com/PHPjc/446065.html www.bkjia.com true http://www.bkjia.com/PHPjc/446065.html techarticle This is like a human having children (why must we have children, is it to prevent the old!) You've got some of your genes and your wife's genes .