Home >Backend Development >PHP Problem >What are the benefits of using classes in php
A class is a collection of many methods. These methods are some logic or algorithms that you often use in your program. Wrapping them into classes can improve program efficiency and reduce code duplication.
For example, if you have a class file web_common.class.php, which contains a class named common, then when your program needs to use the methods in this class, the first thing to do is to copy the class file Pack it in
require_once 'web_common.class.php';
Then, create an object for your class, that is, instantiate the class. (Uninstantiated classes cannot be used, you can try it if you are interested)
$object = new common;
Next, call the method where you need to call the method in the class, such as:
$object->my_function();
Isn’t it very simple? It should also be noted that the static method calling in the class is different from the above calling form. The calling method is as follows:
$object::staticfunction();
Regarding the issue of subclasses inheriting parent classes, there are two possibilities
First, the methods of the parent class are not enough or the program needs a collection of multiple methods of the parent class
Second, the parent class is an abstract class, so if you want to use this type of method, you must It can be used only by inheriting from subclasses
(recommended learning, PHP tutorial)
The inheritance method is as follows:
class son_class extands father_class { }
It needs to be explained that , a parent class can have multiple subclasses, but a subclass can only inherit one parent class
When called, when the parent class is a normal class, the object can be either a parent class or a subclass; the parent class When a class is an abstract class, the object can only be a subclass.
The above is the detailed content of What are the benefits of using classes in php. For more information, please follow other related articles on the PHP Chinese website!