When we implement a project or project, we always want to build a source code that we can read quickly and easily develop for later. And one way is to develop source code according to object orientation.
So we need to understand what object orientation is and the object-oriented characteristics to be able to apply it to a project or project.
II. What is OOP?
OOP or Object Oriented Program is a technique that allows developers to create objects in code, making it easier to manage and develop! Why?
First: Since it is an object, we can more easily imagine it than writing more code.
Second: Because of the management over the object, it will avoid possible duplication.
Because we rely on the concept of “object technology”, in which objects contain attributes and actions or methods. It is these methods that will help objects interact with each other.
Increase productivity, simplify complex when maintaining Easy absorb, easier to learn than previous methods Reduce the amount of code
It is more difficult to read when entering a project using OOP When writing code to handle a certain behavior, instead of writing code to run function A, B to work A and B, if you use OOP, you have to code more 2 functions A and B to do this. However, it is not a problem to maintain later
III. Characteristics of OOP
When working with OOP, you need to know its four basic characteristics:
- Packing properties
We will find out in turn
This is the ability to allow programmers to ignore or not pay attention to some characteristics of an information that it is directly working on, which helps to focus on the core rather than the outer edge. Objects interact with each other without caring about how it works.
This property is most clearly expressed through Interface and Abstract Class
To understand, we will see a small example:
We have Interface BaseRepository with a function gift , this function has an argument that $ age is the number of years
Now we will use a class to implement this interface
In this function we specify if $ age> 18 prints ‘Gift’, otherwise ‘No gifts’.
From here we can see, when we have not defined the behavior of objects, we can build the interface first, then depending on the purpose for which objects implement from that interface to execute it .
The ability to allow a class to inherit properties and methods of another class without redefining them.
This helps the audience share or expand the features available without redefining. For example I have a class Animal has an attribute and a method numberLeg souding
Let Cat class inherit from Animal class (use extends)
Then, the Cat class will have the methods and properties of the Animal class, but we don’t need to redefine it
This feature seems quite ambiguous, even for writers like me. It is understood as follows: When writing an abstract class or interface, it is an instance of polymorphism to rewrite all of these abstract methods. It helps the walls of different classes to express a message in different ways. It has two expressions, one is through Override , the other is Overload .
Override : The fact that a method in the parent class is rewritten by the subclass but preserves the input parameters, only changes the internal processing.
Overload : The fact that a class has multiple methods with duplicate names but different in terms of input or output data types.
In this Animal class, having a dead () method is an abstract method, requiring all extend methods from this method to override it again.
4. Packing Feature
This property does not allow the user to change the intrinsic state of an object. Only the object’s internal methods allow to change its state.
In any way, allowing the external environment to affect the internal data of an object is entirely up to the person writing the code. This is a property that ensures the integrity of the object.
This property is expressed through the use of access modifiers. Normally there will be 3 accesses:
public: Every place can call it protected: Only the class itself and the access subclasses are private: Only the class itself defines can be called
And usually we will leave the properties private, then use the get () methods to retrieve and set () to interact with those properties.
You will ask the question: Why is this property needed? Quite simply, it is like having a person come to your house to borrow a scissors, instead of letting them into the house and tossing the house up to find scissors, then you can get them. The same interaction with properties is similar.
Regardless, these object-oriented model developments will be used in a few years, not knowing if there will be another development model to replace it. Model-based employment not only helps the developer to increase productivity but is also easier to develop and use. The above analysis still has many things that are not correct. Looking forward to receiving everyone’s suggestions