dongshan8953 2016-12-03 15:37
浏览 20

Php,如果只允许从另一个方法调用方法,这是一个坏迹象吗?

its a gaming site, and we store scores:

class Game
{
    public function addScore($player, $score)
    {
        INSERT INTO game .........
    }
}

administrators may erase scores so lets add a removal method too:

public function delete($id)
{
    DELETE FROM game .........
}

now the problem is, deletion is logged, so we must wrap this code:

Controller:

$log->addLog('user deletetion');
$game->delete($id);

So when we delete this game in controller, the logging takes place too. Now here comes the problem: in code, nothing prevents to just call the $game->delete(); method! This is bad, because if any rookie start using this code, he cant know that deletion must come with logging. Is this a sign of something, or??

  • 写回答

3条回答 默认 最新

  • duanqiu2064 2016-12-03 15:57
    关注

    If you always want to write the log before deletion it's completely fine to just add the logging into the delete() method as RiggsFolly suggested as this is an "atomic" operation.

    You would violate SRP if for example apart from logging+deletion you would pop up a dialog in the very same method. As you would mix data handling and UI in the same method. This is two separate responsibility.

    评论

报告相同问题?