Answer the question
In order to leave comments, you need to log in
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() ...
.... // ещё какие-то действия с объектом
}
class Post
{
... // переменные/поля
... // геттеры/сеттеры
... // toString и другие связанные методы
}
Answer the question
In order to leave comments, you need to log in
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
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question