Usage of inheritance during php development

Source: Internet
Author: User

Inheritance
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. Copy codeThe Code is as follows: <? Php
Class test {
Public function _ construct (){
}
Public function name (){
$ This-> xname ('john ');
}
Private function showName ($ name ){
Echo 'My name in test is '. $ name;
}
}
Class extendTest extends test {
Public function _ construct (){
Parent: :__ construct ();
}
Private function showName ($ name ){
Echo 'My name in extendTest is '. $ name;
}
}
$ Test = new extendTest ();
$ Test-> name ();
?>

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:
<? Php
$ Ncart = new Named_Cart; // create a shopping cart with a name.
$ Ncart-> set_owner ("kris"); // name the cart.
Print $ ncart-> owner; // output the name of the shopping cart owner.
$ Ncart-> add_item ("10", 1); // (functions inherited from the shopping cart class)
?>
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.Copy codeThe Code is as follows: class Person {
Protected $ name; // The permission protected by protected. It can be accessed by sub-classes, but cannot be accessed externally.
Protected $ age;
Protected $ sex;
Function _ construct ($ name, $ age, $ sex ){
$ This-> name = $ name; // when this is used,
$ This-> age = $ age;
$ This-> sex = $ sex;
Echo "###############";
}
Public function say (){
Echo "my name: {$ this-> name}, my age {$ this-> age }:, my gender: {$ this-> sex} <br/> ";
}
Protected function eat (){
Echo "wwwwwwwwwwwwwwwwwww <br> ";
}
Function run (){
}
Protected $ name; // The permission protected by protected. It can be accessed by sub-classes, but cannot be accessed externally.
Protected $ age;
Protected $ sex;
}
// Inherit
Class Student extends Person {
Var $ school;
Function _ construct ($ name, $ age, $ sex, $ school ){
Parent: :__ construct (); // call the constructor of the parent class.
$ This-> school = $ school;
}
// Reload the say () method for extension
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.
// Person: say (); // call the say () method of the parent class
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.
Echo "my school {$ this-> school} <br/>"; // www.3ppt.com
}
Function study (){
Echo "{$ this-> name} learning <br/> ";
}
}
$ S = new Student ("zhangsan", 23, "male ");
$ S-> say ();
$ S-> study ();

* 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. The same class can only have one parent class.
*
* No matter whether it is multi-inheritance or single-inheritance, there can be multiple sub-classes
*
* If you have members that can be shared when designing two classes, you can use the shared content separately as a base class.
*
* 1. Application of class inheritance
*
* 1. Declare a subclass and use the extends keyword to inherit (extend) a parent class.
*
* 2. The subclass can inherit all content from the parent class, including the member attribute method and constructor method..., which can be used in the subclass.
*
* 2. Access Type Control
*
* Although the subclass can inherit all content from the parent class, private Members 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. You only need to set the permission to protected.
*
*
*
* 3. Method for overloading the parent class in the subclass
*
* 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 there is a constructor in the parent class, you must call the constructor that is overwritten in the parent class.
*
* 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.