In PHP, you can use the class keyword plus the class name to define a class. The syntax is "[keywords that modify the class] class class name {class attributes and methods;}"; you can use new Keyword to instantiate a class into an object, the syntax is "variable name = new class name (parameter list)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Definition of php class
In PHP, you can use the class keyword plus the class name to define a class, and then use curly brackets { } to wrap the attributes and methods of the class defined in the class body. The syntax format of the class is as follows:
1 2 3 |
|
The naming rules for class names, variable names and function names are similar. They all need to follow the custom naming rules in PHP and can be any legal tag that is not a PHP reserved word. A legal class name starts with a letter or underscore, followed by a number of letters, numbers, or underscores. If the class name consists of multiple words, it is customary to capitalize the first letter of each word. In addition, the class name should have a certain meaning and should not be composed of just a few letters.
The keyword that modifies the class is an optional parameter and can be omitted. We usually use the following keywords to modify classes:
abstract: Abstract class or method. After being modified as an abstract class, the class cannot be instantiated, but can be inherited. If at least one method in a class is declared abstract, then the class must also be declared abstract. When inheriting an abstract class, the subclass must redefine all abstract methods in the parent class, and the access control of these methods must be the same as in the parent class.
final: Classes decorated with final cannot be inherited, and methods decorated with final cannot be redefined in subclasses.
Note: A class can contain its own constants, variables (called "member properties" or "properties" in the class) and functions (called "members" in the class method" or "method").
For example:
Define a car class with attributes including the color and price of the car
1 2 3 4 5 6 7 |
|
Instantiation of the php class
The instantiation of a class is also called creating an object or instantiating an object or instantiating a class.
It is very easy to instantiate a class into an object. Just use the new keyword and follow it with a method with the same name as the class name. Of course, if you do not need to pass parameters for the object when instantiating the object, just use the class name directly after the new keyword without adding parentheses.
The instantiation format of the object is as follows:
1 2 3 |
|
The parameter description is as follows:
Variable name: the reference name of an object created through the class, The members of the object can be accessed through this name;
new: keyword, indicating that a new object is to be created;
Class name: Represents the type of new object;
Parameter list: The constructor of the specified class is used to initialize the value of the object. If there is no constructor defined in the class, PHP will automatically create one without parameters. 's default constructor. (We will introduce it in detail later).
A simple example:
We define a human class and instantiate this class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the definition and instantiation method of php class. For more information, please follow other related articles on the PHP Chinese website!