Hello, Bad idea, slow, really.. But it allows to create,modify persistent variables, etc... Any comments, suggestions?
Alexander ---------------------------------------------------------------------------- ----- use test; CREATE TABLE `tbl` ( 'id' mediumint(8) unsigned NOT NULL, 'version' mediumint(8) unsigned default '1', 'header' varchar(255), 'content' text, PRIMARY KEY ('id') ); INSERT INTO tbl VALUES("1","1","test header","test content"); ---------------------------------------------------------------------------- ----- #!/usr/local/bin/php -q <?php $DB = array('sock'=>':/tmp/mysql.sock','user'=>'root','pass'=>'?????','database'=> 'test'); $DB['link'] = mysql_connect($sock,$DB['user'],$DB['pass']); mysql_select_db($DB[database]); class mpersist { function mpersist($id) { $this->id = $id; $this->version = -1; $this->classname = get_class($this); $this->reload(); } function reload() { echo "::reload $this->classname($this->id,$this->version)\n"; $rt = mysql_query("select * from $this->classname where id=$this->id"); $data = mysql_fetch_assoc($rt); $this->version = $data[version]; $this->data = $data; } function __get($prop_name, &$prop_value) { if($prop_name=='id' or $prop_name=='version' or $prop_name=='classname' or $prop_name=='data') { $prop_value = $this->$prop_name; return true; } $rt = mysql_query("select version from $this->classname where id=$this->id"); $ver = mysql_result($rt,0); if($ver != $this->version) $this->reload(); $prop_value = $this->data[$prop_name]; return true; } function __set($prop_name, $prop_value) { if($prop_name=='id' or $prop_name=='version' or $prop_name=='classname' or $prop_name=='data') { $this->$prop_name = $prop_value; return true; } $val = mysql_escape_string($prop_value); $rt = mysql_query("update $this->classname set $prop_name='$val',version=version + 1 where id=$this->id and version = $this->version"); if(mysql_affected_rows() != 1) { echo "::version too old or something wrong\n"; return false; } else { $this->data[$prop_name] = $prop_value; $this->version++; } return true; } } class tbl extends mpersist{}; overload('tbl'); /* test1 */ /* try to update tbl "update tbl set header='new header', version = version+1 where id=1" from another window or using graphical client :) during execution...*/ $a = new tbl(1); for($i=0;$i<60;++$i) { echo "\n--". date("H:i:s") . "--\n"; echo $a->header; sleep(1); } /* test2 */ /* try to select from tbl "select * from tbl where id=1" from another window or using graphical client :) during execution...*/ for($i=0;$i<60;++$i) { echo "\n--". date("H:i:s") . "--\n"; echo $a->header = "$i iteration"; sleep(1); } ?> -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, visit: http://www.php.net/unsub.php