A class is a collection of objects that have the same properties and methods. Objects are instantiations of classes. Creating a class is very simple. Just use the class keyword plus curly brackets, as follows
Copy after login
as above. In this way, a class named Person is created. Usually, the first letter of the class name needs to be capitalized. of.
To instantiate an object, you need to use the new keyword. Below we instantiate an object and name it $p1
Copy after login
In this way, an instance is created. Of course, this class There are no member variables (that is, attributes) and member methods (that is, functions). We can also add some attributes and methods to this class
Copy after login
Now we will use this class to create three instance objects, namely $p1, $p2, $p3. The advantage of using classes is that objects can be instantiated quickly. With objects, we can implement some methods and functions.
The above is the detailed content of What are the uses of php classes and objects. For more information, please follow other related articles on the PHP Chinese website!