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);
}
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.