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.

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

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果