It should start with the fact that polymorphism is different.
In OOP, polymorphism most often refers to the ability of classes with the same specification (interface) to define different implementations, which, in turn, allows client code to abstract from this implementation and work with the class based on its specification.
For example, your method can expect to receive at the input an object of type UserInterface , while not knowing which particular subtype of type UserInterface it will work with. Thus, you can process different types of data in a uniform way, relying on the fact that each input parameter conforms to the UserInterface specification.
At the same time, in some OOP languages, so-called. "duck typing". She implicit typing. This is when client code expects that a method is defined on the object it uses. This allows you to use polymorphism to process objects that do not even necessarily belong to the inheritance hierarchy.
For example, if I write a function:
function ($object) { echo $object->setName(); }
It will be able to work correctly both with your heirs from user , and with any other object that can call the setName method without parameters, which would return a string. For example:
class DefinitelyNotAUser { public function setName() { return 'haduken!'; } }
The anonymous function described above can work with instances of this class in the same way as with your heirs from user .
This is called signature polymorphism. This approach is widely used in Ruby, in PHP I would not recommend using it.
A function implemented for an arbitrary data type will also be an example of polymorphism (parametric). For example:
function handler($object, callable $action) { return $action($object); } $result = handler(new admin(), function ($object) { echo $object->setName(); });
It is worth noting that polymorphism is not something peculiar exclusively to the object-oriented paradigm. Polymorphism is present in both the functional and procedural paradigm. Parametric polymorphism of the function described for an arbitrary data type is an example for the OP. Calling a specific implementation of a function depending on the types of parameters passed is an example of polymorphism for procedural programming.
The main manifestation of polymorphism is the late binding of the called code to the calling one, when the startup environment is able to determine which implementation is being called based on the context of the program execution.
The simplest definition of polymorphism that you now need to remember, given that PHP and OOP are in the tags to the question: "One interface - many implementations."
What is wrong with your current understanding: you are fixated on the relationship between the base and derived classes. Polymorphism is not about that. It is about the calling (client) code and the called one, as well as their dynamic correspondence.
adminandsuperUserretain the parentCallmethod, 2. Inclusion when the methods for accessing the members of a class are (setName) with which you can regulate access and manage the logic for changing values, and 3. polymorphism when in your examples the base emptysetNameoverridden by different final implementations in the heir classes. - teran