D
D
dev4002016-05-13 22:51:51
PHP
dev400, 2016-05-13 22:51:51

Is this polymorphism?

getSingle(controller) method works with a method from the model
called getSingle

/**

     * This method gets the single page

     * @param array $args

     * @return string

     * @throws \Exception

     */
    public function getSingle(Array $args = [] ) {

        $model = null;

        try {

            $model = $this->model->getSingle("url",  $args);

        } catch(\Exception $e) {

            throw new \Exception("Ошибка");

        }

        return $this->view->render(
            "/site/news/single",
            [
                'model' => $model,
                'msg' => $this->msg
            ],
            "/layouts/news"
        );
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-05-13
@dev400

No.
Polymorphism, as the name suggests, is when something masquerades as something else. It's a property of the type system of the language you're writing in, it may allow you to do things that mask their appearance ("names") but it's still not the same thing. Well, it is worth noting that polymorphism has different types. For example:
Parametric polymorphism . This is when we can write one code, with one set of names, that works with different types of arguments. An example is templates from C++ or generics in Java. That is, the "names" of the methods are the same, because they are in the same instance. One implementation, one behavior. But the arguments may differ.
The key difference from ad-hoc polymorphism, which will be discussed below, is that our implementation has no idea what will come in. Anything can come and it will be necessary to work with it, however, we will work with any type in exactly the same way.
Subtype polymorphism . In this case, we will just have the same names and there may be completely different behavior. For example, we can create some basic type that, for example, sends emails, and create its subtype, which, instead of sending emails, logs what we wanted to send, after which it sends data further to another object of this type (decorator design pattern).
Many people confuse subtype polymorphism with inheritance, substituting these concepts. Without inheritance, of course, we cannot achieve a type hierarchy, but this is more a mechanism than a principle.
Well, there is one more serious limitation. If we want to replace an object of some type in the system with an object of a subtype (roughly speaking, an heir), then the system should not break. That is, the "other" behavior of our subtype must be compatible in terms of interface with the base type. You can read about it on Google "Barbara Liskov's Substitution Principle".
Ad-hoc polymorphism- this is perhaps the most interesting type of polymorphism with which you can chill for a long time. In essence, with this kind of polymorphism, we have the same names, and the behavior depends on the incoming arguments. An example is method overloading in C++. This kind of polymorphism is interesting mainly because it is not "real".
With a dynamic type system, no additional features are required, such as the same method overloading, to achieve ad-hoc polymorphism. Stupidly we throw what we want into the function, and there we already rake with ifs or we lead to one variant. Separate constructs are needed in languages ​​with a static type system. That is, we need to know at the stage of compiling the code which types can come into our methods, and depending on them, call one or another code.
Among PHP developers, there are many who dream of seeing honest method overloading in this language with a dynamic type system, like in Java or C ++. Just like that, because ifs are bad and it's better to let them be implicit at the compiler/runtime level.
Cast polymorphism is another kind of "not real" polymorphism. We "emulate" polymorphism due to the fact that at the runtime level of the language there are casts of the real into the desired. For example, in PHP, we can set a string type hinting for a function, and we can internally have the same behavior for all incoming arguments. We can pass as an argument everything that can be cast into a string.
Actually, this is very similar to parametric polymorphism in meaning, but far from being so flexible and does not give that control over the system.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question