Class is the definition of an object. It contains information about the object action method, including its name, method, attribute, and event. In fact, it is not an object because it does not exist in the memory. When the code of the referenced class is run, a new instance of the class, that is, an object, is created in the memory. Although there is only one class, multiple objects of the same type can be created from this class in the memory. The class is defined by the class keyword.
Php class definition
Class is the definition of an object. It contains information about the object action method, including its name, method, attribute, and event. In fact, it is not an object because it does not exist in the memory. When the code of the referenced class is run, a new instance of the class, that is, an object, is created in the memory. Although there is only one class, multiple objects of the same type can be created from this class in the memory. The class is defined by the class keyword.
Basic syntax:
Class name {// attributes, methods}
Note:
1. define a class (class only)
2. define the class attribute public (public is the modifier, there are three in total, public, protected, private, here we only use public)
For example:
Defines the color and price of a car.
Class car {public $ color; // defines the attribute public $ price ;}
Php class instantiation method
Class instantiation is also called creating an object, instantiating an object, or instantiating a class.
Here is a simple example:
We define a person's class and instantiate this class.
Class Preson {// defines a Preson class public $ name; // defines the class attributes (name, gender, age, etc.) public $ age; public $ gender; // public .....} // new indicates creating a new person and assigning the new object to $ Preson1. this is the instantiation of $ Preson1 = new Preson (); // instantiate the class $ Preson1-> name = "zhang san"; $ Preson1-> age = 22; $ Preson1-> gender = "female"; echo $ Preson1-> name. ''. $ Preson1-> age. ''. $ Preson1-> gender; // If you want to output the second instance, the output is OK. you only need to change the variable name $ Preson2 = new Preson (); // instantiate the class $ Preson2-> name = "Xiao Liang"; $ Preson2-> age = 25; $ Preson2-> gender = "male ";
This section describes the basic concepts and simple instantiation of classes and objects.
The above is the definition and instantiation method of php classes. For more information, see other related articles in the first PHP community!