dongyinju5977 2013-04-20 16:00
浏览 61
已采纳

接口在框架中的位置

Let's say you have a framework with 2 components which are located in separate folders and namespaces. You want them to be decoupled to a level, that each of them can be used on its own.

The first class provides a certain functionality, like writing message into log files:

<?php
namespace \Sample\Log;

class Logger impements LoggerInterface {
public function log($message) { //Write message into log file } }

The second class also provides a framework functionality, but should also be able to write log messages using the provided class.

namespace \Sample\Config;

    class Config
    {

    protected $logger;

    public function setLogger(LoggerInterface $logger)
    {
    $this->logger=$logger
    }

    public function loadConfig(){
    include('config.php')
    $this->logger->log('Config file loaded!');
    }

    }

Where should the interface be located to allow framework users to only use config class? Should they be in Sample/Interfaces/, Sample/Logger/, Sample/Config/ or somewhere else? All three of the options I provided have certain pros and cons, so I'm not really sure where I should put it.

<?php

interface LoggerInterface
{
public function log($message);
}
  • 写回答

1条回答 默认 最新

  • dongxieyou3314 2013-04-20 21:19
    关注

    I would go with Sample/Interfaces if there are many. If not, something like Samples/Shared for instance. But this is really more of a preference or design decision, rather than a rule or something. Meaning: is up to you to decide.

    But, just for the record, there is a standard logger interface that has been accepted by the php framework comunnity:

    https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md

    If you're writing a framework, you should really take a look in all the accepted standards. The first one (psr0) is practicaly mandatory. I'm guessing you are already following it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?