class sort { private $str; public function __construct($str) { $this->str=strtolower($str); } private function explodes(){ if(empty($this->str)) return array(); $arr=explode(" ",$this->str); return is_array($arr)?$arr:array($arr); } public function sort() { $explode=$this->explodes(); sort($explode); return $explode; } } class file { private $sort=null; private $filepath; public function __construct($arrobj,$path) { $this->sort=$arrobj; $this->filepath=$path; } private function getresource($filename,$mode) { return fopen($this->filepath.$filename,$mode); } private function closeresource($resource) { fclose($resource); } public function savefile($filename) { $arr=$this->sort->sort(); $fopen=$this->getresource($filename,"a+"); if(!$fopen){ echo "文件打开失败!"; exit; } var_dump($arr); foreach($arr as $key=>$value) { fwrite($fopen,$value."\n"); } $this->closeresource($fopen); } public function readfile($filename) { $this->savefile($filename); $fopen=$this->getresource($filename,"r"); if(!$fopen){ echo "文件打开失败!";exit; } $arr=array(); while(!feof($fopen)) { $get=fgets($fopen); if(!empty($get)) $arr[]=str_replace("\n","",$get); } $this->closeresource($fopen); return $arr; } } $file=new file(new sort('Apple Orange Banana Strawberry'),"E:\\"); $arr=$file->readfile("fruit.dat"); var_dump($arr);