We need to perform some tasks when creating and destroying objects. For example, when creating an object, assigning a value to a property, closing the data connection when the object is destroyed, and so on, requires constructors and destructors.
constructor Function
- Constructors can accept parameters and be able to assign values to object properties when creating an object
- Constructors can call class methods or other functions
- Constructors can call constructors of other classes
Destructors
- Destructors are calls that are automatically called when an object is destroyed and cannot be displayed.
- Destructors cannot take parameters.
Destructors may be called (but not necessarily) in the following situations:
- After the PHP page has finished loading;
- Unset () class;
- When a variable reference points to another object or value;
The access modifier can be used to decorate the object's methods, and the rules are the same:
Public: The method can be accessed at any scope.
Protected: A method can only be accessed from a member of a class or an inherited class.
Private: The method can only be accessed from one member of the class and cannot be accessed from members of the inheriting class. As with attributes, private methods can be redefined in an inherited class. Each class can see only the private methods it defines itself.
Object-Oriented 3