六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 83|回复: 0

PHP Properties

[复制链接]

升级  92%

12

主题

12

主题

12

主题

童生

Rank: 1

积分
46
 楼主| 发表于 2012-12-10 13:07:01 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body"><div class="cnblogs_code">
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 }
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表