This article mainly introduces three major features of php: encapsulation, inheritance, and polymorphism. It has good reference value. let's take a look at it below. This article mainly introduces three major features of php: encapsulation, inheritance, and polymorphism. It has good reference value. let's take a look at it together with the small editor.
1. encapsulation
Objective: to make the class safer
Practice: if the member variable is private, the member variable is indirectly operated through the method and the restriction conditions are added to the method.
II. Inheritance
Concept: subclass can inherit everything in the parent class.
Method rewriting: override the method of the parent class in the subclass.
Feature: single inheritance: One subclass can have multiple parent classes, and one parent class can derive multiple child classes.
Override: method rewriting
Overload: overload, edit polymorphism
III. polymorphism (running polymorphism)
Concept: The parent class references a subclass instance. because the child class overwrites the method of the parent class, the parent class references different states when calling this method.
Condition:
1 must occur under integration
2. the parent class method must be overwritten.
3. the parent class references the call method.
If a method requires a parent class parameter, you can give a subclass object
Static
Common member
Common members belong to objects.
Static Member
Static members belong to the class
Keyword: static
Self keyword: indicates the class in the class
Common members cannot be called in static methods.
Static members can be called in common methods.
Interface
Extremely abstract class
The interface cannot contain Member Variables. it can only contain member methods.
Member methods can have no function bodies
Interface keyword: interface
The class that implements the interface. all methods in the interface must be implemented.
Loading Class:
include("./Ren.class.php");include "./Ren.class.php";require("./Ren.class.php");require "./Ren.class.php";require_once("./Ren.class.php");require_once "./Ren.class.php";
Automatic class loading method
All class files should be placed in the same directory.
The naming rules of all class files are consistent.