/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
///a generic logger... //polymorphism in action abstract class ILogger { protected abstract function writeLog($sSevirity, $sMsg); } class Logger implements ILogger{ protected function writeLog($sSevirity, $sMsg){ foreach (self::$loggers as $logger){ $logger->writeLog($sSevirity, $sMsg); } } public function registerLogger(ILogger $logger){ } public function logDebug($sMsg){ $this->writeLog("DEBUG", $sMsg); } public function logError($sMsg){ $this->writeLog("ERROR", $sMsg); } public function logInfo($sMsg){ $this->writeLog("INFO", $sMsg); } } abstract class BaseLogger implements ILogger{ private $ipAddr; public function BaseLogger(){ Logger::registerLogger($this); } private function getClientIP(){ //... } } class MyFileLogger extends BaseLogger{ public function MyFileLogger(){ parent::__constructor(); } protected function writeLog($sSevirity, $sMsg){ } } class MyDBLogger extends BaseLogger{ public function MyFileLogger(){ parent::__constructor(); } protected function writeLog($sSevirity, $sMsg){ //connect... $query = "insert into my_logger (sevirity, message) values ('$sSevirity', '$sMsg')"; } } //in use... $logger = new Logger(); $logger->logDebug("miao"); $logger->logInfo("mitzi"); $logger->logError("pizzi");