문제

which one makes more sense?

This one is probably easier to use since you just can do $article->save();

 <?php

class Article
{
    private $author;

    public function __constructor(Author $author)
    {
        $this->author = $author;
    }

    public function save()
    {
        $database = ServiceLocator::getDatabase();
        //save article logic
    }
} 

Here you pass the object, resulting in having the control in you controller (or wherever you use it). Centralizing the control.

 <?php

class Article
{
    private $author;

    public function __constructor(Author $author)
    {
        $this->author = $author;
    }

    public function save(Database $database)
    {
        //Irgendwelche Logik um den Artikel zu speichern mittels $database
    }
} 

I'm prefering the last one, but I'm not entierly sure. Whats the state of art?

Thanks

도움이 되었습니까?

해결책

The article should not know how to save itself. The article now has too many responsibilities. Extract the logic that saves articles into a different class: a repository.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top