In-depth discussion of the Inheritance _php tutorial for PHP classes

Source: Internet
Author: User

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.

 
 
  1. < ? PHP
  2. Class father{
  3. protected $name;
  4. function __construct ($name) {
  5. $this- > name = $name;
  6. }
  7. function __destruct () {
  8. echo " < p>{$this->name} is also going to die < br/>< / p>";
  9. }
  10. This is called a constructor, which is used to initialize
  11. function go_to_sleeping () {
  12. echo " < p>{$this->name} want to sleep. < / P > ";
  13. }
  14. Function Eat () {
  15. echo " < p>{$this->name} want to eat. < / P > ";
  16. }
  17. }
  18. Class Son extends father{
  19. function playing () {
  20. The child will be very naughty, of course, he also wants to eat to sleep the creature
  21. echo " < p>{$this->name} is making Mischief ... < / P > ";
  22. }
  23. }
  24. $ Your_father = New father ("Dad");
  25. $your _father- > go_to_sleeping ();
  26. $your _father- > eat ();
  27. $ My_son = New son (' baby ');
  28. $my _son- > go_to_sleeping ();
  29. $my _son- > eat ();
  30. $my _son- > playing ();
  31. ?>
  32. < ? PHP
  33. Class father{
  34. protected $name;
  35. function __construct ($name) {
  36. $this- > name = $name;
  37. }
  38. function __destruct () {
  39. echo " < p>{$this->name} is also going to die < BR/>< / p>";
  40. }
  41. This is called a constructor, which is used to initialize
  42. function go_to_sleeping () {
  43. echo " < p>{$this->name} want to sleep. < / P > ";
  44. }
  45. Function Eat () {
  46. echo " < P > {$this->name} want to eat. p>";
  47. }
  48. }
  49. Class Son extends father{
  50. function playing () {
  51. The child will be very naughty, of course, he also wants to eat to sleep the creature
  52. echo " < p>{$this->name} is making Mischief ... < / P > ";
  53. }
  54. }
  55. $ Your_father = New father ("Dad");
  56. $your _father- > go_to_sleeping ();
  57. $your _father- > eat ();
  58. $ My_son = New son (' baby ');
  59. $my _son- > go_to_sleeping ();
  60. $my _son- > eat ();
  61. $my _son- > playing ();
  62. ?>

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 .

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