M
M
Max Ba2018-05-19 16:14:11
PHP
Max Ba, 2018-05-19 16:14:11

Should I do inheritance or not?

I can't understand at all. In OOP examples, they often write that there is a User class and UserStandart, UserAdmin heirs, well, something like this, + -.
And I have a User class, in which all control methods are immediately registered. And what is the status of the user, written in the property: 1 regular user, 2 admin.
And I don’t understand why to do inheritance in this case, for the sake of an example, or does it make sense?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
V
Vasyl Fomin, 2018-05-19
@fomvasss

If you always have an instance of the User class that has the same methods and fields, then there is no need to make separate classes. Moreover, specifically in your example, the user's functionality can be separated by roles / rights, which can be implemented separately later.

A
Arman, 2018-05-19
@Arik

if($user instanceof UserAdmin) {
//admin
}
public function openWebsite(UserAdmin $userAdmin) {
// 
}

We know for sure that the user is an admin or we ask that the user be only an admin, how the check for admin is implemented there, this code does not apply.

Y
Yan-s, 2018-05-19
@Yan-s

It is necessary or not to be determined by you designing the architecture of the application. Examples are written to explain the point that inheritance is when one object is a more specific kind of another.

P
Peter, 2018-05-19
@Morpheus_God

You may have only the Name field and the LastName field described in the User class.
And in a specific implementation, you can add your own methods and additional fields for each specific user. Everything depends on the project.

A
Alexander Getmansky, 2018-05-19
@JimmDiGrizli

There is a good way to determine the rationality of using inheritance. To do this, you need to answer a number of questions:
- Both classes are from the same subject area;
- the heir meets the Barbara Liskov substitution principle;
- the code of the inherited class is necessary or well suited for the successor;
- the heir basically adds logic.
The more positive answers, the more likely you will not miss the choice of inheritance.
Specifically, in your case, I would venture to suggest that the use of inheritance is redundant, and can even become a time bomb as the system develops further. Here, inheritance acts as a choice of possible algorithms for user actions in the system, and the introduction of new roles can be very expensive, especially if one user will have to have several roles at once.

V
Vapaamies, 2018-05-22
@vapaamies

Inheritance is used to encapsulate validation isAdmin. Roughly speaking, all if s that perform the check turn into VMT records (or whatever in PHP).
The architecture may depend on the number of roles. It's one thing to check only for the admin, and another thing for a bunch of different roles. The code with a bunch of switches is actually VMT emulation, it is better to solve this problem architecturally using OOP.
Another argument against OOP is the nature of the code. For example, in most methods, the actions of the admin are in addition to the actions of a simple user, and then if- You don't look so bad. Although, if there is a lot of additional code, and you want to see the differences in the admin role as a logically unified implementation, and not as a set of disparate additions, it is better to use OOP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question