Class declaration:
The code is as follows: |
Copy code |
<? Php Permission modifier class name {// permission modifier: public, protected, private, or omitted 3. // Class body; // class is the key word for creating a class } // The class name must be followed by the class and followed by the member of the class placed. ?> // Ps: you can add a permission modifier before the class keyword, static, abstract, and other keywords. A class, that is, all the content between a pair of braces must be in a piece of code, and the content in the class cannot be divided into blocks. <? Php Class ConnDB { //.... ?> <? //... }; ?> |
Member attributes:
The variables directly declared in the class are called member attributes/Variables. They can be of the scalar type and compound type in php, and the resource type and null type are invalid.
In addition, when declaring a member attribute, you must have a keyword to modify it: public, protected, private; no specific significance is required: var. when declaring a member attribute, there is no need to assign an initial value.
Member constants:
Modified with a const constant, for example, const PI = 3.1415926;
Constant output does not need to be instantiated. It can be directly called by class name + constant name. The format is: class name: constant name
Ps. Special access methods: -------- "$ this" and "::"
1) $ "this" exists in each member method, which is a special object to use. the member method belongs to that object, and the $ this application represents that object. Its function is to complete access between members of the object.
2) ":" becomes the scope operator. With this operator, you can call constants, variables, and methods in the class without creating objects. The syntax format is as follows:
Keywords: variable name/constant name/method name
Keyword: parent, which can call the member variables, member methods, and constants in the parent class member;
Self, which can call static members and constants in the current class;
Class name, which can call constants, variables, and methods in the class;
Member method:
A function declared in a class becomes a member method. Multiple functions can be declared in a class, that is, an object can have multiple member methods. the declaration of the member method is the same as that of the function. The only special feature is that the member method can be modified with keywords to control its access permissions.
Class instantiation
Creation object:
$ Variable name = new class name ([parameter]); // class instantiation.
Member class:
$ Variable name-> member attribute = value;