PDA

View Full Version : گفتگو: کلاس ذخیره اطلاعات در فایل



Tarragon
پنج شنبه 27 تیر 1392, 15:23 عصر
سلام
بنده کلاسی جهت ذخیره اطلاعات در فایلی نوشتم اگر نظری در مورد کلاس دارید خیلی خوش حال می شم تا بشنوم.
خیلی ممنونم:

<?php
class save{
public $name,$dir;
protected $template;
const el = PHP_EOl;
public function __construct($dir,$name){
$this->dir = $dir;
$this->name = $name;
$this->template = "<?php".self::el."<string>\t\$name = \$value;".self::el."</string>?>";
}
protected function variable($array){
if(is_array($array)){
$template = explode("<string>",$this->template);
$template[1] = explode("</string>",$template[1]);
$ntemplate = $template[0];
foreach($array as $key => $value){
$ntemplate .= str_replace(array('name','value'),array($key,$valu e),$template[1][0]);
}
$ntemplate .= $template[1][1];
return $ntemplate;
}else{
echo "You must send a array!";
}
}
public function write($array,$start = true){
$mode = (($start) ? 'w+' : 'a+');
$fp = fopen($this->dir.$this->name, $mode) or die("can't open file");
$text = $this->variable($array);
fwrite($fp, $text);
fclose($fp);
chmod($this->dir.$this->name, 0444);

}
}