标签:
<?php
class Message{
private $message;
function __construct($m){
$ip = $_SERVER(‘REMDTE_ADDR‘);
$this->message = $ip.‘:‘.$m;
}
function getMessage(){
return $this->message;
}
}
//适配器类,不改变原有类的情况下.增加新的借口
class MessageAdapter extends Message{
private $ip;
private $text;
function __construct(){
//适配器往往需要调用父类的某些方法
parent::__construct($m){
$message = $this->getMessage();
$arr = explode(":", $message);
$this->ip = $arr[0];
$this->text = $arr[1];
}
}
function getip(){
return $this->ip;
}
function getText(){
return $this->text;
}
}
class LogFile{
public static function write($messageObj){
$message->$messageObj->getMessage();
$h = fopen(‘./log.log‘, ‘a‘);
fwrite($h, $message."\n");
fclose($h);
}
}
class LogCSV{
public static function write($messageObj){
$ip = $messageObj->getIp();
$text = $messageObj->getText();
$message = $ip.‘,‘.$text;
$h = fopen(‘./log.csv‘, ‘a‘);
fwrite($h, $message."\n");
fclose($h);
}
}
$m = new MessageAdapter(‘this is a log!!‘);
LogFile::write($m);
标签:
原文地址:http://www.cnblogs.com/meibao/p/5146865.html