Php basics about how to use inheritance. Inheritance is one of the three object-oriented mechanisms. It exists in C ++, JAVA, and PHP. The following describes how to use PHP. These classes are usually needed.
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.
-
- 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:
-
- $ 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.
- 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}
";
- }
- Protected function eat (){
- Echo "wwwwwwwwwwwwwwwwwwwww
";
- }
- 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}
"; // Www.3ppt.com
- }
- Function study (){
- Echo "{$ this-> name} is learning
";
- }
- }
- $ S = new Student ("zhangsan", 23, "male ");
- $ S-> say ();
- $ 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.
Pipeline is one of the three object-oriented mechanisms. It exists in C ++, JAVA, and PHP. The following describes how to use PHP. These classes are usually needed...