Home > Article > Backend Development > Detailed explanation of php object-oriented_encapsulation_PHP tutorial
Below we use examples and tutorials to explain PHP object-oriented and encapsulation, as well as the declaration of various encapsulations.
Chapter 7 (5) Detailed explanation of object-oriented_encapsulation
Below we use examples and tutorials to explain PHP object-oriented and encapsulation, as well as the declaration of various encapsulations.
Encapsulation: It is to encapsulate the properties or methods inside the object inside the own object. They can be used or accessed inside the object, but the encapsulated members cannot be used outside the object or in other objects.
Keywords used for encapsulation: private
The meaning of encapsulation:
1. Combine all properties and methods of the object to form an indivisible independent unit (object).
2. Information hiding, that is, hiding internal details as much as possible, forming a boundary (or forming a barrier to the outside), and retaining only a limited external interface so that it can relate to the outside.
Chapter 7 (6) Detailed explanation of object-oriented_Four commonly used methods used in encapsulation
The method with "__" added to the function is a method that is automatically called at a certain moment.
__get (the name of the attribute) calls the __get method when directly calling the private attribute.
__set(1.Attribute name 2.Attribute value) Call the __set method when directly assigning a value to a private attribute.
__isset (attribute name) calls the __isset method when determining whether the private attribute inside the object exists.
__unset (attribute name) calls the __unset method when deleting private attributes inside the object.
Chapter 7 (7) Detailed explanation of object-oriented_inheritance
Inheritance of classes: Inheritance is the extension of the original class.
Newly extended subclass: 1.class 2.Original parent class name 3.extends inheritance keyword 4.New subclass name.
Class inheritance means inheriting all the contents of the parent class to the subclass, except for private attributes and methods.
Chapter 7 (8) Detailed explanation of object orientation_Method coverage and access
Overloading of class methods: The method name must be the same as that of the parent class. If you want to call the entire method of the parent class, you can use the keyword pr]arent:: in the subclass method. If you want to call only the part, rewrite it.
Access type:
private private can only be accessed in this class.
protected protected can only be accessed in this class and subclasses.
public public is not restricted and can be accessed in this class, subclasses and outside.
Chapter 7 (9) Detailed explanation of object-oriented_Commonly used final, static, const keywords
final: can only be used to define classes and methods.
Function: Classes defined using final cannot be inherited.
Methods defined using final cannot be overridden by overloading.
static: Define properties and methods as static.
static can modify properties and methods.
Non-static members cannot be used in methods declared using static.
The function of static is that it only needs to be declared once in the class. All objects generated by this class have static-modified properties and methods, but the memory will not be used repeatedly in the memory, which can effectively save memory.
To access statically defined properties or methods, use classname::defined properties or methods, or use felf::defined properties or methods to access. Objects cannot be used for direct access.
The function of const is to define member attributes as constants in the class.
const can only modify attributes.
const can only be initialized when declared.
Use const to declare attributes without adding $.
To access the attributes defined by const, use class name::defined attributes to access. The object cannot be accessed directly.