PHP Basics about how to use inheritance _php tutorial

Source: Internet
Author: User
Tags php basics vars
Inheritancebelongs to one of the three main mechanisms of object-oriented. In C++,java, PHPAre present, the following is a description of how PHP is used.

Typically, these classes are required to have the same variables and functions as other existing classes. In fact, it would be a good practice to define a generic class for all projects and enrich this class to fit each specific project. 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 known as "inheritance", but no one dies), and contains all the parts defined in the derived classes.

The elements in a class cannot be reduced, that is, you cannot unregister any existing functions or variables. An extension class always relies on a single base class, that is, multiple inheritance is not supported. Use the keyword "extends" to extend a class.

 
 
  1. class Test {
  2. Public function __construct () {
  3. }
  4. Public function name () {
  5. $this ->xname (' John ');
  6. }
  7. Private function showname ($name) {
  8. Echo ' My name in test is '. $name ;
  9. }
  10. }
  11. class extendtest extends Test {
  12. Public function __construct () {
  13. Parent::__construct ();
  14. }
  15. Private function showname ($name) {
  16. Echo ' My name in Extendtest '. $name ;
  17. }
  18. }
  19. $test = New extendtest ();
  20. $test ->name ();
  21. ?>

The above example defines a class named Named_cart that owns all the variables and functions of the Cart class, plus the additional variable $owner and an additional function, Set_owner (). Now, the normal way to create a name shopping cart, and can set up and get the owner of the cart. and the normal shopping cart function can still be used in the name of the shopping cart class:

 
  
  
  1. $ncartNew
  2. $ncart ->set_owner ("Kris"
  3. $ncart
  4. $ncart ->add_item ("Ten"
  5. ?>

This can also be called a "parent-child" relationship. Create a class, 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 only be used after they are defined! If you need class Named_cart to inherit the class cart, you must first define the cart class. If you need to create another Yellow_named_cart class based on the Named_cart class, you must first define the Named_cart class. Simply stated: The Order of class definitions is very important.

 
 
  1. class person{
  2. protected $name; //protected protected permissions, which can be accessed by subclasses, cannot be accessed externally
  3. protected $age;
  4. protected $sex;
  5. function __construct ($name,$age,$sex) {
  6. $this ->name= $name ; //When using this, even if name is not declared, it will again declare a
  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}
    ";
  13. }
  14. protected function eat () {
  15. Echo "wwwwwwwwwwwwwwwwwwwww
    ";
  16. }
  17. function run () {
  18. }
  19. protected $name; //protected protected permissions, which can be accessed by subclasses, cannot be accessed externally
  20. protected $age;
  21. protected $sex;
  22. }
  23. //Inheritance
  24. class Student extends person{
  25. var $school;
  26. function __construct ($name,$age,$sex, $school) {
  27. parent::__construct (); //Call the construction method of the parent class
  28. $this ->school= $school ;
  29. }
  30. //Overload say () method to extend
  31. protected function say () {//Parent class Use public, subclass permissions cannot be lower than parent class, can drink the same permissions as parent class
  32. //person::say ();//Call the Say () method of the parent class
  33. Parent::say (); //Call the parent class say () method, which represents the parent class name and can be called when the parent class name changes.
  34. Echo "My School {$this->school}
    "; //www.3ppt.com
  35. }
  36. function Study () {
  37. Echo "{$this->name} is learning
    ";
  38. }
  39. }
  40. $s = New Student ("Zhangsan", "a","M");
  41. $s ->say ();
  42. $s ->study ();

Benefits of Inheritance:

1. One of the three main features of object-oriented

2. Openness and extensibility

3. Increase the reusability of the Code

4. Improved maintainability of the software

5. Inheritance is to use subclasses to "extend" the parent class

C + + is multi-inheritance, and the same class can have more than one parent class

PHP and Java are single-inheritance, with only one parent class in the same class

Whether multiple inheritance or single inheritance, you can have multiple subclasses

As long as you are designing two classes, there are members that can be shared, and the content that can be shared is used 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. Subclasses can inherit all content from the parent class, including the member property method, the constructor method ..., which can be used in subclasses.

II. type of access control

Although subclasses can inherit all content from a parent class, private, privately-owned members can only be used in this class, and they cannot be used in subclasses.

When encapsulating, you can make the internal of your class accessible, and the subclass can be used, but the outside of the class cannot be used, as long as the permission is set to protected

Methods of overloading parent classes in subclasses

1. Subclasses can declare a method name that can be declared with the same parent class, that is, a subclass overrides a method with the same name as the parent class

2. Methods of subclasses extend to parent class methods

3. Calling the overridden method in the parent class in a subclass

Use parent class Name:: Method Name () Parent:: Method Name ()

4. Write the constructor method in the subclass, and if there is a constructor in the parent class, make sure to call the overridden constructor in the parent class

Note: Overloaded methods in subclasses cannot be lower than access permissions in the parent class (subclasses can magnify permissions but not reduce permissions)

Hope to be of help to you.


http://www.bkjia.com/PHPjc/445786.html www.bkjia.com true http://www.bkjia.com/PHPjc/445786.html techarticle inheritance belongs to one of the three major mechanisms of object-oriented. In C++,java, PHP is present, the following is a description of how PHP is used. It is often necessary to have such classes, these classes ...

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