PHP Properties


1
class Properties{ 2 protected $props=array(); 3 protected $change=false; 4 protected $path=""; 5 public function loadfromfile($path){ 6 $path=trim($path); 7 if($path==""){ 8 return false; 9 }else{ 10 $this->path=$path; 11 $val = file($path); 12 if(false===$val){ 13 return false; 14 }else{ 15 foreach($val as $v){ 16 list($key,$value)=explode("=",$v,2); 17 if(trim($key)=="")continue; 18 $this->props[$key]=$value; 19 } 20 } 21 } 22 } 23 public function get($key){ 24 $key=trim($key); 25 if($key=="")return false; 26 return $this->props[$key]; 27 } 28 public function set($key,$value){ 29 $key=trim($key); 30 if($key=="")return false; 31 $value=trim($value); 32 if($this->props[$key]==$value) 33 return true; 34 $this->props[$key]=$value; 35 $this->change=true; 36 return true; 37 } 38 public function writeToFile($path){ 39 if(!isset($path)){ 40 if(!$this->change) 41 return true; 42 $path=$this->path; 43 } 44 $path=trim($path); 45 if($path=="")return false; 46 $fstr=""; 47 foreach($this->props as $i=>$j){ 48 $fstr .= $i." = ".$j."\n"; 49 } 50 if(file_put_contents($path,$fstr,LOCK_EX)===false){ 51 return false; 52 }else{ 53 return true; 54 } 55 } 56 }
posted @ 2012-10-23 08:14  InnerPeace  阅读(308)  评论(0)    收藏  举报