T
T
Taras Serevann2016-03-25 19:03:38
PHP
Taras Serevann, 2016-03-25 19:03:38

How to properly implement dependency inversion in PHP?

Let's say there is a project - a wrapper over the social API. networks. It has a Wall class (for working with the user's wall) and a Post class (contains post data, their getters/setters and other methods).
Wall class:

class Wall
{
    public function addPost($text) 
    {
        ... // какие-то действия
        return new Post($id, $text);
    }

    public function getLastPost()
    {
        ... // какие-то действия в результате которых мы можем:
        return new Post($last['id'], $last['text']);
    }

    // предположим, метод, который делает репост или другие действия, не суть
    public function repost(Post $post) 
    {
        ... $post->getId() ...
        ... $post->toString() ...
        .... // ещё какие-то действия с объектом 
    }

Post class:
class Post
{
    ... // переменные/поля
    ... // геттеры/сеттеры
    ... // toString и другие связанные методы
}

This shows that the Wall class is very closely related to the Post class (especially where new instances are created in the Wall class) and this does not comply with SOLID and in particular the principle of dependency inversion.
How to loosen the coupling between classes and make them more portable and SOLID? And is it necessary to do this in such a situation? I basically have ideas, but I'm sure there is some elegant pattern that will solve all my problems and which I don't know about yet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Nesmeyanov, 2016-03-25
@Taras_Serevann

Specify not an object, but its interface. In this case, there are three methods - getId, toString and __construct. This will be enough to get you started.
PS
In a good way, you still need to get rid of the id, because this field is implementation specific, not logic specific. All received posts from any storage will have it, and newly created posts do not need to know about its existence. Those. it should look something like this:

interface Post
{
    public function __construct(string $content, User $user);
    public function getContent() : string;
    public function getUser() : User;
    public function changeAuthor(User $user);
}

class WallPost implements Post
{
    ... implement methods
}

In this case, any posts will have only the author and content, and the rest will be the subtleties of implementation, because. id is needed only for the database to associate the user with the post.
But this is all a slightly different story (see domain-oriented programming).
PPS At the expense of setters, I would argue. I propose to look at this https://habrahabr.ru/post/279919/#comment_8816651 comment thread, where Sergey explained to me in an accessible way about their need and I have to agree with this, because the immutability of the data is good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question