<?php
class Log()
{
$protected writer = false;
function __construct($writer)
{
$this->write = $writer;
}
public function log($message)
{
if( ($message == "") || ($this->write == false) )
{
return;
}
$message = date("Y-m-d H:i:s")." - ".$message."rn";
$this->writer->log($message);
}
}
class fileWriter
{
public function log($message)
{
file_put_contents("./fileWriter.log", $message, FILE_APPEND);
}
}
class databaseWriter
{
public function log($message)
{
$db->insert("log_table", array("message"), array($message) );
}
}
// do some stuff
$oFileWriter = new fileWriter();
$oDatabaseWriter = new databaseWriter();
// log to file:
$oLog = new Log($oFileWriter);
$oLog->log("This logs to a file");
// log to database
$oLog = new Log($oDatabaseWriter);
$oLog->log("This logs to a database");