Introduction to the use of inheritance in object-oriented classes in PHP

Source: Internet
Author: User
Tags php language
The inheritance of PHP classes is an important point of knowledge in the learning of PHP language. So how do we properly learn the inheritance of PHP classes? In PHP object-oriented programming, class inheritance is always the most critical.

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)

Grammar:

Class father{}

1. Produce "Descendants" (subclass)

Grammar:

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 a dying <br/></p>";   }//This is called the constructor, 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 () {//child will be very naughty, of course he is also to eat the creature to sleep echo "<p>{$this->name} is playing tricks.   .</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 a dying <br/></p>";  }//This is called the constructor, 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 () {//child will be very naughty, of course, he also want to eat the creature to sleep 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.

<?phpclass My_class{var $username = "Chicken"; var $num =10;function Yingxiaoji () {//Use global variables in class $num= $this->num;$ Username= $this->username;echo "I keep". $num. " Article ". $username;   } function Jiegou () {//Use the functions in the class with this direct reference $this->yingxiaoji (); echo "All sold";}} The extends of the PHP class represents inheritance, but PHP does not allow the simultaneous inheritance of multiple base classes class My_class_extend extends My_class{var $color = "Red"; function MyColor () {$   color= $this->color;   $username = $this->username; echo ", this only". $username. " Is ". $color;}} $myname =new My_class (); The instantiation of the class $myname->yingxiaoji (); echo "<br>";//$myname->username= "duckling" for variables in class; $myname->num= "20"; $myname->yingxiaoji (); echo "<br>"; $myname->jiegou (); echo "<br>";//initializes the inheriting class of a class $myname_extend=new My_class_extend (); $myname _extend->jiegou (); $myname _extend->mycolor (); echo "<br>";//Because it is an inheritance class, Then its base class properties all have, can directly give the value of the base class variable $myname_extend->username= "duckling"; $myname _extend->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.