To achieve a record of the operation history of the function
- and undo, anti-undo feature similar to a feature. (Implement the forward and backward of the operation)
- and Discuz forum to view post (you can go back to view the post, as well as Post view history)
- the logical and Windows Explorer address bar forward and backward functions are the same.
based on this requirement, a data structure is implemented. Write a generic class, called The history class. "The principle and the clock are similar. When instantiating an object, you can construct a ring with a length of n (which can be fixed as long as you want). then integrate the various operations. Forward, backward, insert, modify insert. class can construct an array. Or an object is constructed by passing in an array parameter. After each operation, the array after the operation can be obtained. End of Operation The data can be saved in the right way according to your needs. put it in cookie,session, or serialize it, or convert it to JSON data in a database, or in a file. easy to use next time.
For ease of expansion, more data is stored. Each individual piece of data is also an array record. For example, expand as needed: Array (' path ' = ' d:/www/', ' SSS ' =>value) ----------------------------------------------------------------------------
By the way, write your own debug variable with a file.
- PR () can format and highlight output variables. PR ($arr), PR ($arr, 1) is output after exiting.
- debug_out () is used to output multiple variables. The default is exit.
- debug_out ($_get,$_server,$_post, $arr);
-
- Include ' debug.php ';
- /**
- * History Operation Class
- * Pass in or construct an array. Shaped like:
- Array
- Total number of ' history_num ' =>20,//queue nodes
- ' First ' =>0,//starting position, starting from 0. Array index value
- ' Last ' =>0,//end position, starting from 0.
- ' Back ' =>0,//the number of steps backwards from first position, the difference.
- ' History ' =>array (//array, which holds the operation queue.
- Array (' path ' = ' d:/'),
- Array (' path ' = ' d:/www/'),
- Array (' path ' = ' e:/'),
- Array (' path ' = '/home/')
- ......
- )
- )
- */
- Class history{
- var $history _num;
- var $first;
- var $last;
- var $back;
- var $history =array ();
- function __construct ($array =array (), $num =12) {
- if (! $array) {//array is empty. Constructs a circular queue.
- $history =array ();
- for ($i =0; $i < $num; $i + +) {
- Array_push ($history, Array (' path ' = '));
- }
- $array =array (
- ' History_num ' = $num,
- ' First ' =>0,//start position
- ' Last ' =>0,//end position
- ' Back ' =>0,
- ' History ' and $history
- );
- }
- $this->history_num= $array [' History_num '];
- $this->first= $array [' first '];
- $this->last= $array [' last '];
- $this->back= $array [' Back '];
- $this->history= $array [' History '];
- }
- function Nextnum ($i, $n =1) {//loop n a value. Similar to the clock loop.
- Return ($i + $n) < $this->history_num? ($i + $n):($i + $n-$this->history_num);
- }
- function Prevnum ($i, $n =1) {//Loop previous value I. Rolls back N positions.
- Return ($i-$n) >=0? ($i-$n): ($i-$n + $this->history_num);
- }
- function minus ($i, $j) {//Clockwise two points only, i-j
- Return ($i > $j)? ($i-$j):($i-$j + $this->history_num);
- }
- function Gethistory () {//returns an array for saving or serializing operations.
- Return Array (
- ' History_num ' = $this->history_num,
- ' First ' = $this->first,
- ' Last ' = $this->last,
- ' Back ' = $this->back,
- ' History ' = $this->history
- );
- }
- function Add ($path) {
- if ($this->back!=0) {//has a back action record, insert.
- $this->goedit ($path);
- Return
- }
- if ($this->history[0][' path ']== ') {//Just construct, do not add one. First not move forward
- $this->history[$this->first][' path ']= $path;
- Return
- }else{
- $this->first= $this->nextnum ($this->first);//First move forward
- $this->history[$this->first][' path ']= $path;
- }
- if ($this->first== $this->last) {//Start and end position meet
- $this->last= $this->nextnum ($this->last);//The end position is moved forward.
- }
- }
- function GoBack () {//returns the address of n step back from first.
- $this->back+=1;
- The maximum number of back steps is the difference between the starting point and the end point (clockwise difference)
- $mins = $this->minus ($this->first, $this->last);
- if ($this->back >= $mins) {//rewind to last point
- $this->back= $mins;
- }
- $pos = $this->prevnum ($this->first, $this->back);
- return $this->history[$pos [' Path '];
- }
- function GoNext () {//step backward from First n step.
- $this->back-=1;
- if ($this->back<0) {//rewind to last point
- $this->back=0;
- }
- return $this->history[$this->prevnum ($this->first, $this->back) [' Path '];
- }
- function Goedit ($path) {//back to a point, without advance but modified. The firs value is the last value.
- $pos = $this->minus ($this->first, $this->back);
- $pos = $this->nextnum ($pos);//Next
- $this->history[$pos [' Path ']= $path;
- $this->first= $pos;
- $this->back=0;
- }
- Whether you can back
- function Isback () {
- if ($this->back < $this->minus ($this->first, $this->last)) {
- return ture;
- }
- return false;
- }
- Is it possible to move forward
- function Isnext () {
- if ($this->back>0) {
- return true;
- }
- return false;
- }
- }
- Test the code.
- $hi =new History (Array (), 6);//pass in an empty array, initialize the array construct.
- for ($i =0; $i <8; $i + +) {
- $hi->add (' s '. $i);
- }
- PR ($hi->goback ());
- PR ($hi->goback ());
- PR ($hi->goback ());
- PR ($hi->gonext ());
- PR ($hi->gonext ());
- PR ($hi->gonext ());
- PR ($hi->gonext ());
- $hi->add (' asdfasdf ');
- $hi->add (' asdfasdf2 ');
- PR ($hi->gethistory ());
- $ss =new History ($hi->gethistory ());//Direct array construction.
- $ss->add (' asdfasdf ');
- $ss->goback ();
- PR ($ss->gethistory ());
- ?>
Copy Code
-
- /**
- * Get the name of the variable
- * EG hello= "123" gets the SS string
- */
- Function Get_var_name (& $aVar) {
- foreach ($GLOBALS as $key = $var)
- {
- if ($aVar = = $GLOBALS [$key] && $key! = "ARGC") {
- return $key;
- }
- }
- }
- /**
- * Format output variable, or object
- * @param mixed $var
- * @param boolean $exit
- */
- function Pr ($var, $exit = False) {
- Ob_start ();
- $style = " ;
- if (Is_array ($var)) {
- Print_r ($var);
- }
- else if (Is_object ($var)) {
- echo Get_class ($var). " Object ";
- }
- else if (Is_resource ($var)) {
- Echo (string) $var;
- }
- else{
- echo Var_dump ($var);
- }
- $out = Ob_get_clean ();//buffered output to $out variable
- $out =preg_replace ('/' (. *) "/', '" '. ' \\1 '. ' "', $out);//Highlight string variable
- $out =preg_replace ('/=\> (. *)/', ' = '. ' '.' \\1 '.', $out);//Highlight and the value behind
- $out =preg_replace ('/\[(. *) \]/', '['. ' \\1 '. ' ]', $out);//Highlight variable
- $from = Array (', ' (', ') ', ' = = ');
- $to = Array (', '(', ')', ' == ');
- $out =str_replace ($from, $to, $out);
- $keywords =array (' array ', ' int ', ' String ', ' class ', ' object ', ' null ');//keyword highlighting
- $keywords _to= $keywords;
- foreach ($keywords as $key = $val)
- {
- $keywords _to[$key] = ". $val.";
- }
- $out =str_replace ($keywords, $keywords _to, $out);
- echo $style. '
'. Get_var_name ($var). ' = '. $out. ' ';
- if ($exit) exit;//is true Then exit
- }
- /**
- * Debug output variable, the value of the object.
- * Any parameter (any type of variable)
- * @return Echo
- */
- function Debug_out () {
- $avg _num = Func_num_args ();
- $avg _list= Func_get_args ();
- Ob_start ();
- for ($i =0; $i < $avg _num; $i + +) {
- PR ($avg _list[$i]);
- }
- $out =ob_get_clean ();
- Echo $out;
- Exit
- }
- ?>
Copy Code |