Php inheritance details

Source: Internet
Author: User
Inheritance: This class is usually required. these classes have the same variables and functions as other existing classes. In fact, a general class is defined for all projects, it is a good practice to enrich the class to adapt to each specific project.

Inheritance:This class is usually required. these classes have the same variables and functions as other existing classes. In fact, a general class is defined for all projects, it is a good practice to constantly enrich the class to adapt to each specific project. to make this easier, the class can be extended from other classes, the extended or derived class has all the variables and functions of its base class (this is called "inheritance", but no one is dead) and contains the sections defined in all derived classes, the elements in the class cannot be reduced. that is to say, you cannot cancel any existing functions or variables. an extended class always depends on a separate base class. that is to say, it does not support multi-inheritance, use the keyword "extends" to extend a class.

  1. Class test {
  2.  
  3. Public function _ construct (){
  4. }
  5.  
  6. Public function name (){
  7. $ This-> xname ('John ');
  8. }
  9.  
  10. Private function showName ($ name ){
  11. Echo 'my name in test is '. $ name;
  12. }
  13. }
  14.  
  15. Class extendTest extends test {
  16.  
  17. Public function _ construct (){
  18. Parent: :__ construct ();
  19. }
  20.  
  21. Private function showName ($ name ){
  22. Echo 'my name in extendTest is '. $ name;
  23. }
  24. }
  25.  
  26. $ Test = new extendTest ();
  27. $ Test-> name ();
  28. ?>

The above example defines the class named Named_Cart, which has all the variables and functions of the Cart class, plus the additional variable $ owner and an additional function set_owner (). now, A shopping cart with a name can be created in a normal way and the owner of the shopping cart can be set and obtained. functions of the normal shopping cart class can still be used in the shopping cart class with a name:

  1. $ Ncart = new Named_Cart; // create a shopping cart with a name.
  2. $ Ncart-> set_owner ("kris"); // name the cart.
  3. Print $ ncart-> owner; // output the name of the shopping cart owner.
  4. $ Ncart-> add_item ("10", 1); // (functions inherited from the shopping cart class)
  5. ?>

This can also be called the "parent-child" relationship. create a class and parent class, and use extends to create a new class based on the parent class: subclass, you can even use this new subclass to create another class based on this subclass.

Note:Classes can be used only after they are defined. if the class Named_Cart inherits the class Cart, you must first define the Cart class. if you need to create another Named_Cart class based on the Named_Cart class, you must first define the Named_Cart class, to put it simply, the sequence of class definitions is very important.

  1. Class Person {
  2. Protected $ name; // The permission protected by protected. it can be accessed by sub-classes, but cannot be accessed externally.
  3. Protected $ age;
  4. Protected $ sex;
  5.  
  6. Function _ construct ($ name, $ age, $ sex ){
  7. $ This-> name = $ name; // when this is used,
  8. $ This-> age = $ age;
  9. $ This-> sex = $ sex;
  10.  
  11. Echo "###############";
  12. }
  13.  
  14. Public function say (){
  15. Echo "my name: {$ this-> name}, my age {$ this-> age }:, my gender: {$ this-> sex}
    ";
  16. }
  17.  
  18. Protected function eat (){
  19.  
  20. Echo "wwwwwwwwwwwwwwwwwwwww
    ";
  21.  
  22. }
  23.  
  24. Function run (){
  25.  
  26. }
  27. Protected $ name; // The permission protected by protected. it can be accessed by sub-classes, but cannot be accessed externally.
  28. Protected $ age;
  29. Protected $ sex;
  30. }
  31.  
  32. // Inherit
  33. Class Student extends Person {
  34. Var $ school;
  35.  
  36. Function _ construct ($ name, $ age, $ sex, $ school ){
  37. Parent: :__ construct (); // call the constructor of the parent class.
  38. $ This-> school = $ school;
  39. }
  40.  
  41. // Reload the say () method for extension
  42. Protected function say () {// the parent class uses public. the permission of the subclass cannot be lower than that of the parent class, and the permission of the parent class can be the same.
  43. // Person: say (); // call the say () method of the parent class
  44. Parent: say (); // call the statement () method of the parent class. parent indicates the parent class name. it can also be called when the parent class name changes.
  45.  
  46. Echo "My school {$ this-> school}
    ";
  47. }
  48.  
  49. Function study (){
  50. Echo "{$ this-> name} is learning
    ";
  51. }
  52.  
  53. }
  54.  
  55. $ S = new Student ("zhangsan", 23, "male ");
  56. $ S-> say ();
  57. $ S-> study ();
  58. ?>

1. one of the three features of object-oriented

2. openness and scalability

3. added code reusability.

4. improved software maintainability

5. inheritance is to "extend" the parent class with sub-classes.

C ++ is multi-inheritance. the same class can have multiple parent classes.

PHP and JAVA belong to single inheritance, and the same class can only have one parent class

No matter how many inheritance or single inheritance, you can have multiple subclasses.

When you design two classes and have members that can be shared, you can use the shared content separately as a base class.

I. Application of class inheritance

1. declare a subclass and use the extends keyword to inherit (extend) a parent class

2. subclass can inherit all content from the parent class, including the member attribute method and constructor method..., which can be used in the subclass.

II. access type control

Although the subclass can inherit all content from the parent class, the private member can only be used in this class, and not in the subclass.

During encapsulation, the class can be accessed internally or used by subclass, but the class cannot be used externally, as long as the permission is set to protected

III. Methods for overloading parent classes in child classes

1. the subclass can declare the same method name as the parent class, that is, the subclass overwrites the method with the same name as the parent class.

2. extension of the subclass method to the parent class method

3. call the override method in the parent class in the subclass.

Use the parent class name: method name () parent: method name ()

4. Compile the constructor in the subclass. if the parent class also has constructor, you must call the constructor that is overwritten in the parent class once.

Note:The method that is overloaded in the subclass cannot be lower than the access permission in the parent class (the subclass can enlarge the permission, but cannot narrow down the permission ).

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.