<?php
require_once 'memento.php';
require_once 'send_peoper.php';
require_once 'user.php';
class main{
public function main(){
//程序主入口
$send_peoper = new send_peoper();
$user = new user();
$send_peoper->__set('status','好的状态');
//产生备份文件,并且给对象的属性,这个用户对象就保留了
//一个原始版本的对象
echo '【备份】你当前的系统状态是 : '.$send_peoper->__get('status').'<br/>';
$user->create_memento($send_peoper->create_memento());
//
$send_peoper->__set('status','坏的状态');
echo '【现状】你当前的系统状态是 : '.$send_peoper->__get('status').'<br/>';
//用户发出命令对系统进行恢复
$send_peoper->recover_memento($user->recover_memento());
echo '【还原】你当前的系统状态是 : '.$send_peoper->__get('status');
}
}
?>
<?php
/**
* 备忘录类
* @author lichaoyong 2014-04-18 14:53:12
*/
class memento{
private $status;
protected $name;
public $age;
public function __construct(){
// echo 'construct';
}
public function __destruct(){
// echo 'destory';
}
public function __get($property_name){
return $this->$property_name;
}
public function __set($property_name,$property_value){
$this->status = $property_value;
}
public function to_string(){
print $this->status;
}
/**
* 返回对象名称
* __CLASS__
* 还有get_class
*/
public function get_class_name(){
return get_class($this);
}
public static function getinstance(){
static $obj;
if(!$obj){
$obj = new memento();
}
return $obj;
}
}
?>
<?php
/**
* 发起人类
* @author lichaoyong 2014-04-18 14:52:54
*/
class send_peoper{
private $status;
public function __construct(){
// echo 'this is send_peoper construct';
}
public function __destruct(){
// echo 'this is send_peoper destruct';
}
public function __get($property_name){
return $this->$property_name;
}
public function __set($property_name,$property_value){
$this->$property_name = $property_value;
if(!class_exists('memento')){
die('没找到该类') ;
exit;
}
$memento = memento::getinstance();
$memento->__set('status',$property_value);
}
public function to_string(){
print $this->status;
}
//创建一个备忘录
public function create_memento(){
if(!class_exists('memento')){
die('没找到该类') ;
exit;
}
$memento = memento::getinstance();
$r = new ReflectionClass($memento);
$property_bool = $r->hasProperty('status');//反射判断类中是否拥有该属性
$property_private = $r->getProperties(ReflectionProperty::IS_PRIVATE);//访问该类的所有私有属性
$property_public = $r->getProperties(ReflectionProperty::IS_PUBLIC);//访问该类的所有私有属性
$property_protected = $r->getProperties(ReflectionProperty::IS_PROTECTED);//访问该类的所有私有属性
if(!$property_bool){
die('没有找到该类的属性');
exit;
}
return $memento;
}
/**
* 一个备忘录对象
* @param object[memento] $obj
*/
public function recover_memento($obj){
if(!is_object($obj) || get_class($obj) != 'memento'){
die('不是memento对象');
}
$this->status = $obj->__get('status');
}
}
?>
<?php
/**
* 负责人类
*/
class user{
private $memento;
public function __construct(){
// echo 'this is recover construct';
}
public function __destruct(){
// echo 'this is recover destruct';
}
public function recover_memento(){
if(!class_exists('memento')){
die('没找到该类');
exit;
}
// $memento = new memento();
//property_exists($memento,'status') or die('没有找到该类的属性');
//$memento->__set('status','当前的状态值');
// return $this->memento = $memento;
return $this->memento;
}
public function create_memento($memento){
//判断是否是备忘录类
if(!is_object($memento) || get_class($memento) != 'memento'){
die('不是memento对象');
}
// $memento->__set('status','当前的状态值');
//因为里面是static,这里要实现java或者c#中new一个新对象,保留起值的这种效果
//需要克隆一个对象(深度或者浅度)
$this->memento = clone $memento;
}
}
?>
看见别人的博客用c#来写个备份设计模式,自己也用php来写个