Php basics about how to use inheritance

Source: Internet
Author: User
Tags php basics

InheritanceIt is one of the three major object-oriented mechanisms. In C ++, JAVA,PHPAll exist. The following describes how to use PHP.

These classes are usually required. These classes have the same variables and functions as other existing classes. In fact, defining a general class for all projects and constantly enriching the class to adapt to each specific project is a good exercise. To make this easier, classes 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 part defined in all the 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, it does not support multi-inheritance. Use the keyword "extends" to extend a class.

 
 
  1. <?php  
  2. class test {  
  3. public function __construct() {   
  4. }  
  5. public function name() {   
  6. $this->xname('John');   
  7. }  
  8. private function showName($name) {   
  9. echo 'my name in test is '.$name;   
  10. }   
  11. }  
  12. class extendTest extends test {  
  13. public function __construct() {   
  14. parent::__construct();   
  15. }  
  16. private function showName($name) {   
  17. echo 'my name in extendTest is '.$name;   
  18. }   
  19. }  
  20. $test = new extendTest();   
  21. $test->name();   
  22. ?> 

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 is created in a normal way, and the shopping cart owner can be set and obtained. Functions of the normal shopping cart class can still be used in the shopping cart class with the name:

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

This can also be called the "Parent-Child" relationship. Create a class, parent class, and use extends to create a new class: subclass Based on the parent class. You can even use this new subclass to create another class based on this subclass.

Note:

Class can only be used after definition! If the class Named_Cart is required to inherit the class Cart, the Cart class must be defined first. To create another Yellow_named_cart Class Based on the Named_Cart class, you must first define the Named_Cart class. Simply put, 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. Function _ construct ($ name, $ age, $ sex ){
  6. $ This-> name = $ name; // when this is used,
  7. $ This-> age = $ age;
  8. $ This-> sex = $ sex;
  9. Echo "###############";
  10. }
  11. Public function say (){
  12. Echo "my name: {$ this-> name}, my age {$ this-> age }:, my gender: {$ this-> sex} <br/> ";
  13. }
  14. Protected function eat (){
  15. Echo "wwwwwwwwwwwwwwwwwww <br> ";
  16. }
  17. Function run (){
  18. }
  19. Protected $ name; // The permission protected by protected. It can be accessed by sub-classes, but cannot be accessed externally.
  20. Protected $ age;
  21. Protected $ sex;
  22. }
  23. // Inherit
  24. Class Student extends Person {
  25. Var $ school;
  26. Function _ construct ($ name, $ age, $ sex, $ school ){
  27. Parent: :__ construct (); // call the constructor of the parent class.
  28. $ This-> school = $ school;
  29. }
  30. // Reload the say () method for extension
  31. 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.
  32. // Person: say (); // call the say () method of the parent class
  33. 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.
  34. Echo "my school {$ this-> school} <br/>"; // www.3ppt.com
  35. }
  36. Function study (){
  37. Echo "{$ this-> name} learning <br/> ";
  38. }
  39. }
  40. $ S = new Student ("zhangsan", 23, "male ");
  41. $ S-> say ();
  42. $ S-> study ();

Advantages of inheritance:

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)

Hope to help you.


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.