Is there a possibility of inheriting two classes in PHP?
I have a class that already inherits the class Usuarios
and would like it to inherit the class Crud
as well.
class Alunos extends Usuarios {
}
Is there a possibility of inheriting two classes in PHP?
I have a class that already inherits the class Usuarios
and would like it to inherit the class Crud
as well.
class Alunos extends Usuarios {
}
Prefer composition to inheritance.
You do not need to inherit. Just instantiate the classes inside it and use.
In PHP there is no support for multiple inheritance.
You can not, almost any language allows, and even those that allow is problematic .
There is the possibility of using interfaces
(see too ) that defines contracts and makes a multiple inheritance species , subtype, but not subclass, so there is no code reuse, or else traits
which reuses code in a limited way. It is not the same as inheriting from a class, but it helps.
In any case, almost every problem that a person thinks he needs to inherit from more than one class is abusing his inheritance, and then it would be good for him to rethink his whole understanding of inheritance, probably this person inheritance where it should not even in simple cases.
I find it odd a% with% being a Aluno
, you can be, but it is not common. Usuario
is certainly not Aluno
. I do not think it's even a simple inheritance. In fact, no composition between these two things would fit.
That's why I always repeat that almost all OOP code is wrong, in general it has more confusion of concepts than solution for problems. I say more, if people start using OOP right they start giving up.
In PHP there is a feature called Traits. This is a programming method (shared by other languages too) to add more functionality to a class.
If the goal is for multiple classes to use CRUD methods, it might be a good way out.