1. Definition of Class
- The class keyword + class name +{}, in curly braces, defines variables and methods.
- The variables of the class are declared with Var, or the values can be initialized.
- After a class is created, you can use the new operator to instantiate an object of that class.
- Note: The variable $this the object that represents itself.
2. member functions
- Constructor: Used to initialize an object when it is created, to assign an initial value to an object member variable, and to use it with the new operator in the statement that creates the object.
void construct ([ mixed $args [, $... ]] )
- destructor: When an object ends its life cycle, such as when the object's function has been called, the system automatically executes the destructor.
void destruct ( void )
3. Inheritance
- PHP uses the keyword extends to inherit a class that does not support multiple inheritance.
Class A extends b{} //a classes inherit the class B
- PHP does not automatically invoke the constructor of the parent class in the constructor method of the subclass. To execute the constructor of the parent class, you need to call Parent::construct () in the constructor method of the child class.
- Method overrides: If a method inherited from a parent class does not meet the requirements of a subclass, it can be overridden, a procedure called method overrides, also known as a method.
4. Access control
- The access control of a property or method is implemented by PHP by adding the keyword public (publicly), protected (protected), or private.
- Class properties must be defined as public, protected, and private. If defined with Var, it is considered public.
- A method in a class can be defined as public, private, or protected. If these keywords are not set, the method defaults to public.
5. Interface
- Specifies which methods a class must implement, but does not need to define the specific contents of these methods.
- The interface is defined by the interface keyword, and the method defined must be public.
Interface A{} * //声明一个A接口*
- To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. A class can implement multiple interfaces, separating the names of multiple interfaces with commas.
class B implements A,C //实现A,C接口
Basic knowledge of PHP: Object-Oriented Programming