Domanda

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

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top